Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

call of thread  RSS feed

 
Manju Rao
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all please look the following program

class Counter implements Runnable {

private int currentValue;

private Thread worker;

public Counter(String threadName) {
currentValue = 0;
worker = new Thread(this, threadName); // (1) Create a new thread.
System.out.println(worker);
worker.start(); // (2) Start the thread.
}

public int getValue() { return currentValue; }

public void run() { // (3) Thread entry point
try {
while (currentValue < 5) {
System.out.println(worker.getName() + ": "+currentValue++));
Thread.sleep(250); // (4) Current thread sleeps.
}
} catch (InterruptedException e) {
System.out.println(worker.getName() + " interrupted.");
}
System.out.println("Exit from thread: " + worker.getName());
}
}

public class Client {
public static void main(String[] args) {
Counter counterA = new Counter("Counter A"); // (5) Create a thread.

try {
int val;
do {
val = counterA.getValue(); // (6) Access the counter value.
System.out.println("Counter value read by main thread: " + val);
Thread.sleep(1000); // (7) Current thread sleeps.
} while (val < 5);
} catch (InterruptedException e) {
System.out.println("main thread interrupted.");
}

System.out.println("Exit from main() method.");
}
}

output from the program:

Thread[Counter A,5,main]
Counter value read by main thread: 0
Counter A: 0
Counter A: 1
Counter A: 2
Counter A: 3
Counter value read by main thread: 4
Counter A: 4
Exit from thread: Counter A
Counter value read by main thread: 5
Exit from main() method.

In the above program the output in first 2 line should be
Thread[Counter A,5,main]
Counter A: 0
because in the constructor of Counter we start the thread

Please tell me the flow.

Regards
Manju
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

When you have more than one thread, there is more than one "flow", and you can't generally predict how they will overlap. If you start a new thread B from code in another thread A (here, A is the main thread), then you can, in most cases, expect a few lines of code A to execute on A before the run() method starts to execute on thread B. That's what's happening here: at line 6 you create and start a new Counter thread, but before line 3 (or the following lines) can execute, main continues on to execute the line after 6, and line 7. It's probably during that sleep() that the new thread really starts to run.

But the point is that not only can't you say for sure, but that it isn't going to be the same on every run. Your program probably will print what you expected, once in a while.
 
Manju Rao
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ernest Friedman-Hill,

Your reply helped me to clear my concepts. Thank you for giving me time and help.

Regards
Manju
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!