GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt from scjp book on threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt from scjp book on threads" Watch "doubt from scjp book on threads" New topic
Author

doubt from scjp book on threads

Abhishek Mantri
Greenhorn

Joined: Sep 06, 2007
Posts: 25
1. class Reader extends Thread {
2. Calculator c;
3.
4. public Reader(Calculator calc) {
5. c = calc;
6. }
7.
8. public void run() {
9. synchronized(c) {
10. try {
11. System.out.println("Waiting for calculation...");
12. c.wait();
13. } catch (InterruptedException e) {}
14. System.out.println("Total is: " + c.total);
15. }
16. }
17.
18. public static void main(String [] args) {
19. Calculator calculator = new Calculator();
20. new Reader(calculator).start();
21. new Reader(calculator).start();
22. new Reader(calculator).start();
23. calculator.start();
24. }
25. }
26.
27. class Calculator extends Thread {
28. int total;
29.
30. public void run() {
31. synchronized(this) {
32. for(int i=0;i<100;i++) {
33. total += i;
34. }
35. notifyAll();
36. }
37. }
38. }
"The program starts three threads" is this statement correct ,i think it starts 4 threads.This code is from scjp book page 724-725.
Wim Molenberghs
Greenhorn

Joined: Jan 31, 2008
Posts: 16
The books doesn't say "the program starts three threads" but "the program starts three threads that are all waiting to receive the finished calculation"

I can be wrong but the way I read this is that there are 3 threads that are waiting + a thread to finish the calculation.

And the program has not a total of 4 threads but 5 :-)
Abhishek Mantri
Greenhorn

Joined: Sep 06, 2007
Posts: 25
which is the fifth thread then???
do you mean main thread?
Wim Molenberghs
Greenhorn

Joined: Jan 31, 2008
Posts: 16
yes, the main is also a thread.
don't forget this for the exam.
Abhishek Mantri
Greenhorn

Joined: Sep 06, 2007
Posts: 25
Thank you Wim !
 
GeeCON Prague 2014
 
subject: doubt from scjp book on threads