Top DevX Stories
Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Search the forums:
  #1  
Old 09-28-2009, 10:48 AM
Atanu_Banerjee Atanu_Banerjee is offline
Registered User
 
Join Date: Aug 2008
Posts: 20
Red face Thread Basics

Dear Members :

Trying to have some grip on thread, so could you please explain this
behavior ?
My code :

Code:
public class TestThread1 implements Runnable {

       String threadName;
       int count = 0;

       public TestThread1(String name) {
         threadName = name;
      
       } 

       public void run() {
         try {

           for(;;) {

             System.out.println(threadName);
             Thread.sleep(1000);
             count = count + 1;
             System.out.println(count);
           }

          } catch(InterruptedException ex) {

              System.out.println("Sleep Interrupted");

            } 

        } // run()

         
        public static void main(String[] args) {
          TestThread1 runner1 = new TestThread1("First Thread");
          Thread thread1 = new Thread(runner1); 

          TestThread1 runner2 = new TestThread1("Second Thread");
          Thread thread2 = new Thread(runner2); 

          //thread1.setPriority(Thread.NORM_PRIORITY);
          //thread2.setPriority(Thread.NORM_PRIORITY);

          thread2.start();
          thread1.start(); 

        }


}
The output :
F:\JavaPrograms\THREAD>java TestThread1
Second Thread
First Thread
1
Second Thread
1
First Thread
2
Second Thread
2
First Thread
3
Second Thread
3
First Thread
4
Second Thread
4
First Thread
5
Second Thread
5

My Questions :
[a] Second thread starts run() prints "Second Thread", goes to sleep. First
thread starts run(), prints "First Thread" and goes to sleep. "Second
Thread" wakes up by this time, prints count [1], then prints it's name
"Second Thread", goes to sleep. "First Thread" starts, from where it left,
in for loop, prints count[1], prints it's name, goes to sleep.
Am I right thus far ?
Is it separate instances of run() method for the thread1 and thread2, that
we are seeing "own" count values ?

Next I changed my code as follows:

Code:
public class TestThread1 implements Runnable {

       String threadName;
       int count = 0;

       public TestThread1(String name) {
         threadName = name;
      
       } 

       public void run() {

           for(;;) {

             System.out.println(threadName);
             count = count + 1;
             System.out.println(count);

           }

        } // run()

         
        public static void main(String[] args) {
          TestThread1 runner1 = new TestThread1("First Thread");
          Thread thread1 = new Thread(runner1); 

          TestThread1 runner2 = new TestThread1("Second Thread");
          Thread thread2 = new Thread(runner2); 

          //thread1.setPriority(Thread.NORM_PRIORITY);
          //thread2.setPriority(Thread.NORM_PRIORITY);

          thread2.start();
          thread1.start(); 

        }

}
The output :
F:\JavaPrograms\THREAD>java TestThread3
Second Thread
First Thread
1
Second Thread
1
First Thread
2
Second Thread
2
First Thread
3
Second Thread
3
First Thread
4
First Thread
4
Second Thread
5
First Thread
5
Second Thread

[b] The output is on expected line, even removing sleep (as always mentioned
in demo examples). If the threads are of same priority, why one thread
needs to be slept in order to awaken the other ? Alternate
pattern is maintained thruout, except a repetition of "First Thread" - is
that explainable or just a glitch of OS ?

Now I changed the code as follows:
Code:
public class TestThread3 implements Runnable {

       String threadName;
       int count = 0;

       public TestThread3(String name) {
         threadName = name;
      
       } 

       public void run() {

           for(;;) {

             System.out.println(threadName);
             count = count + 1;
             System.out.println(count);

           }

        } // run()

         
        public static void main(String[] args) {
          TestThread1 runner1 = new TestThread1("First Thread");
          Thread thread1 = new Thread(runner1); 

          TestThread1 runner2 = new TestThread1("Second Thread");
          Thread thread2 = new Thread(runner2); 

          thread1.setPriority(Thread.NORM_PRIORITY-1);
          thread2.setPriority(Thread.NORM_PRIORITY+5);

          thread2.start();
          thread1.start(); 

        }

}
The output :

F:\JavaPrograms\THREAD>java TestThread3
Second Thread
First Thread
1
Second Thread
1
First Thread
2
Second Thread
2
First Thread
3
Second Thread
3
First Thread
4
Second Thread
4
First Thread

[c] So nothing changed. I was expecting more of "Second Thread". Why not ?

I am trying clear any misconception of thread I acquired - could someone please show me the way - I appreciate very much.

Thanks in advance.
Atanu Banerjee
Reply With Quote
  #2  
Old 10-13-2009, 08:00 AM
rohitsharma52 rohitsharma52 is offline
Registered User
 
Join Date: Aug 2009
Posts: 5
Hi Atanu,

You are right about your understandings.

Case 1 & Case 2 - without any specific priority given to any thread, every thread is having the same priority and have an equal chance of getting a handle on the resources.

This is the reason why you don't see a fixed pattern. Remember this pattern is somewhat controlled in first case due to sleep but again this is not guaranteed.

Case 3 - with priorities defined, yes the thread with higher priority gets handle first than a thread with lower priority.

Now something funny - why doesn't your observation support the above findings?

notice that both in case 2 and case 3 though you have defined new classes 'TestThread2' and 'TestThread3', but .......you are still using 'TestThread1' inside the main method of each class.

Regards.
Reply With Quote
  #3  
Old 10-14-2009, 06:34 AM
Atanu_Banerjee Atanu_Banerjee is offline
Registered User
 
Join Date: Aug 2008
Posts: 20
Smile Thread Basics

Hi Rohit :

Thanks for pointing that out. Now coming to the revised output :

Code:
class TestThread1 implements Runnable {


       String threadName;
       int count = 0;


       public TestThread1(String name) {
         threadName = name;
      
       } 



       public void run() {

           for(;;) {

             System.out.println(threadName);
             count = count + 1;
             System.out.println(count);

           }

        } // run()

} // TestTheread1


class TestThread2 implements Runnable {

       String threadName;
       int count = 0;

       public TestThread2(String name) {
         threadName = name;
      
       } 

       public void run() {

           for(;;) {

             System.out.println(threadName);
             count = count + 1;
             System.out.println(count);

           }

        } // run()

} // TestTheread2

 public class TestThreadPrior {
         
        public static void main(String[] args) {
          TestThread1 runner1 = new TestThread1("First Thread");
          Thread thread1 = new Thread(runner1); 

          TestThread2 runner2 = new TestThread2("Second Thread");
          Thread thread2 = new Thread(runner2); 

          thread1.setPriority(Thread.NORM_PRIORITY);
          thread2.setPriority(Thread.NORM_PRIORITY+2);

          thread1.start();
          thread2.start(); 

        } 

} // TestPriorTest
The output pieces :

Second Thread
1
Second Thread
2
Second Thread
3
Second Thread
4
Second Thread
5
Second Thread
6
Second Thread
7
Second Thread
8
Second Thread
9
Second Thread
10
Second Thread
........
Second Thread
53
Second Thread
First Thread
1
First Thread
2
First Thread
3
54
Second Thread
55
Second Thread
56
Second Thread
57
Second Thread
58
Second Thread
59
Second Thread
60
....
Second Thread
90
Second Thread
91
Second Thread
92
Second Thread
93
....
and no more "First Thread" seen. Is this on expected line ? I was expecting a mix of alternate threads, with more of "Second Thread". I am in favor of avoiding sleep to see how JVM takes care of naturally.

Thanks.
Atanu
Reply With Quote
  #4  
Old 10-14-2009, 07:07 PM
Atanu_Banerjee Atanu_Banerjee is offline
Registered User
 
Join Date: Aug 2008
Posts: 20
Smile Thread Basics

Hi Rohit :

One more observation. Changing the following, makes no difference in output, to the last one.

Code:
public class TestThreadPrior {
         
        public static void main(String[] args) {
          TestThread1 runner1 = new TestThread1("First Thread");
          Thread thread1 = new Thread(runner1); 

          TestThread1 runner2 = new TestThread1("Second Thread");
          Thread thread2 = new Thread(runner2); 

          thread1.setPriority(Thread.NORM_PRIORITY);
          thread2.setPriority(Thread.NORM_PRIORITY+2);

          thread1.start();
          thread2.start(); 

        }
It's like whether I want multiple threads based on SAME Runnable instance or multiple threads having individual Runnables, thus different {data and code} space.

This opens me to another idea of trying, using extends Thread and see the outcome.

Regards.
Atanu
Reply With Quote
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Thread - Join method solomon_13000 Java 2 07-22-2009 05:53 AM
Problem with thread priorities erray C++ 8 10-11-2007 05:17 AM
Is there any way to start thread with out extending Thread class and implementing Run manik_gopal Java 4 04-27-2006 08:01 AM
Thread getting NoClassDefFoundError? CJ Java 1 10-29-2000 05:51 PM
Thread getting NoClassDefFoundError CJ Java 2 10-27-2000 04:07 PM


All times are GMT -4. The time now is 11:52 PM.


Sponsored Links



Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.