• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

call of thread

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
We must storm this mad man's lab and destroy his villanous bomb! Are you with me tiny ad?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic