aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in 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 in Threads" Watch "Doubt in Threads" New topic
Author

Doubt in Threads

Shruti Rao
Greenhorn

Joined: Feb 01, 2010
Posts: 9
Hello everyone,

Can anyone explain me the flow of the following 2 programs???

class Chicks {
4. synchronized void yack(long id) {
5. for(int x = 1; x < 3; x++) {
6. System.out.print(id + " ");
7. Thread.yield();
8. }
9. }
10. }
11. public class ChicksYack implements Runnable {
12. static Chicks c;
13. public static void main(String[] args) {
14. new ChicksYack().go();
15. }
void go() {
17. c = new Chicks();
18. new Thread(new ChicksYack()).start();
19. new Thread(new ChicksYack()).start();
20. }
21. public void run() {
22. c.yack(Thread.currentThread().getId());
23. }
24. }
Which are true? (Choose all that apply.)

Ans: C. The output could be 4 4 2 2 (HOW???)
E. The output could be 2 2 4 4 (HOW???)





public class Chess implements Runnable {
4. public void run() {
5. move(Thread.currentThread().getId());
6. }
7. // insert code here
8. System.out.print(id + " ");
9. System.out.print(id + " ");
10. }
11. public static void main(String[] args) {
12. Chess ch = new Chess();
13. new Thread(ch).start();
14. new Thread(new Chess()).start();
15. }
16. }
And given these two fragments:
I. synchronized void move(long id) {
II. void move(long id) {
When either fragment I or fragment II is inserted at line 7, which are true? (Choose all that apply.

Ans: C. With fragment I, the output could be 4 2 4 2 (HOW???)
E. With fragment II, the output could be 2 4 2 4 (HOW ???)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

Shruti please Quote Your Sources when you post a question. If you don't mention the source properly, your questions might be deleted. Quoting source is compulsory at javaranch so please comply with this rule...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Ankit Garg wrote:Shruti please Quote Your Sources when you post a question. If you don't mention the source properly, your questions might be deleted. Quoting source is compulsory at javaranch so please comply with this rule...


Please follow the Ranch rules!

Try it.



|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Shruti Rao
Greenhorn

Joined: Feb 01, 2010
Posts: 9
The question is from K&B book,Self Test Question number 16 and 17,Threads chapter
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

Shruti quoting source doesn't mean putting your question is [quote] [/quote] tags. It means just telling us from where you got the question...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in Threads