• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Threads

 
Shruti Rao
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
Shruti Rao
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question is from K&B book,Self Test Question number 16 and 17,Threads chapter
 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shruti quoting source doesn't mean putting your question is [quote] [/quote] tags. It means just telling us from where you got the question...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic