Description of how i want the output : I want two threads Gaurav and john to complete a while loop(from 1 to 8), such that whichever thread starts ist, runs for 5 iterations(i.e. till count=5) , then go to sleep and then next thread completes the loop (run for count=6 to count=8). Program should end then. For this scenario , i created a instance variable count and incrementing its value in counter method
Problem is : i am getting a weird output (output attached at the end ) even after synchronizing the counter method (counter method increments count variable)
public class ThreadCounter implements Runnable {
private int count;
@Override
public void run(){
try {
counter(); // call synchronized counter function
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}//end of run()
public synchronized void counter() throws InterruptedException{
while(true){
// i want the loop to increment count from 1 to 4 by ist thread(whichever thread runs ist) and then 5 to 8 by next thread
System.out.println("Which thread running ? " +Thread.currentThread().getName());
count++;
System.out.println("count is"+count);
if (count==5){
System.out.println("count is"+count +" and Thread is " +Thread.currentThread().getName());
// when count is 5 make the current thread should go to sleep
Thread.currentThread().sleep(7000);
}// if count==5 ends here
//whichever thread starts now , should start the counter from count=5
if (count==8){
break;
// when count is 5 make the current thread go to sleep
}
}// while loop ends here
}// end of counter()
}// end of class ThreadingIst
public class ThreadTest {
public static void main(String [] args){
Thread t1= new Thread(new ThreadingIst());
Thread t2= new Thread(new ThreadingIst());
t1.setName("John");
t2.setName("Gaurav");
t1.start();
t2.start();
}// main ends here
}
Output is :
Which thread running ? John count is1
Which thread running ? John count is2
Which thread running ? John count is3
Which thread running ? John count is4
Which thread running ? John count is5
count is5 and Thread Johnis going to sleep
Which thread running ? Gaurav count is1
Which thread running ? Gaurav count is2
Which thread running ? Gaurav count is3
Which thread running ? Gaurav count is4
Which thread running ? Gaurav count is5
count is5 and Thread Gauravis going to sleep
Which thread running ? Gaurav count is6
Which thread running ? Gaurav count is7
Which thread running ? Gaurav count is8
count is8 and Thread Gauravis coming out of loop
Which thread running ? John count is6
Which thread running ? John count is7
Which thread running ? John count is8
count is8 and Thread Johnis coming out of loop
I have gone through an answer - "implements Runnable" vs. "extends Thread" , in which one of the comment is However, one significant difference between implementing Runnable and extending Thread is that by extending Thread, each of your threads has a unique object associated with it, whereas implementing Runnable, many threads can share the same object instance.
so , if threads can share the same object , then the instance value like count should be shared by both. Why is my output like this then.