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