Thread Basics


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Thread Basics

Hybrid View

  1. #1
    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

  2. #2
    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.

  3. #3
    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

  4. #4
    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

Similar Threads

  1. Thread - Join method
    By solomon_13000 in forum Java
    Replies: 2
    Last Post: 07-22-2009, 04:53 AM
  2. Problem with thread priorities
    By erray in forum C++
    Replies: 8
    Last Post: 10-11-2007, 04:17 AM
  3. Replies: 4
    Last Post: 04-27-2006, 07:01 AM
  4. Replies: 1
    Last Post: 10-29-2000, 04:51 PM
  5. Replies: 2
    Last Post: 10-27-2000, 03:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center