This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes thread question 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 "thread question" Watch "thread question" New topic
Author

thread question

Niala Nirell
Ranch Hand

Joined: Mar 12, 2008
Posts: 46
this question comes from sun's sample questions for 310-065
and i modified it :

public class Lockdown implements Runnable {
public static void main(String[] args) {
Lockdown lock = new Lockdown();
new Thread(lock).start();
new Thread(lock).start();
}
public void run() { locked(Thread.currentThread().getId()); }
synchronized void locked(long id) {
System.out.print(id + "a ");
System.out.print(id + "b ");
}
}

What is true about possible sets of output from this code?
A. Set 6a 7a 7b 8a and set 7a 7b 8a 8b are both possible.
B. Set 7a 7b 8a 8b and set 6a 7a 6b 7b are both possible.
(Two different Lockdown objects are using the locked() method.)
C. It could be set 7a 7b 8a 8b but set 6a 7a 6b 7b is NOT possible.
D. It could be set 7a 8a 7b 8b but set 6a 6b 7a 7b is NOT possible.
E. None of the above

What is the good answer? Thank you
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1765
    
    7

A is incorrect; although set 7a 7b 8a 8b is possible, set 6a 7a 7b 8a is not, because locked is invoked by two distinct threads, which would make three distinct thread id's impossible.

B incorrect; although set 7a 7b 8a 8b is possible, set 6a 7a 6b 7b is not possible, because locked() is synchronized, which means it can be accessed by at most one thread at a time making it impossible for the id's to be ordered this way.

C is correct; set 7a 7b 8a 8b can occur, while set 6a 7a 6b 7b can not for the previously stated reason.

D is incorrect; set 6a 6b 7a 7b is in fact the only set that could occur.

E is incorrect, because C is.
[ March 27, 2008: Message edited by: Jelle Klap ]

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Niala Nirell
Ranch Hand

Joined: Mar 12, 2008
Posts: 46
Thank you very much
[ March 27, 2008: Message edited by: Alain Sellerin ]
Sandeep Bhandari
Ranch Hand

Joined: Apr 16, 2004
Posts: 201
good question. Enjoyed cracking it.


SCJP 96% | SCWCD 90%| SCJP mock exams | My SCJP blog
Avi Sridhar
Ranch Hand

Joined: May 31, 2007
Posts: 42
Originally posted by Jelle Klap:
A is incorrect; although set 7a 7b 8a 8b is possible, set 6a 7a 7b 8a is not, because locked is invoked by two distinct threads, which would make three distinct thread id's impossible.

B incorrect; although set 7a 7b 8a 8b is possible, set 6a 7a 6b 7b is not possible, because locked() is synchronized, which means it can be accessed by at most one thread at a time making it impossible for the id's to be ordered this way.

C is correct; set 7a 7b 8a 8b can occur, while set 6a 7a 6b 7b can not for the previously stated reason.

D is incorrect; set 6a 6b 7a 7b is in fact the only set that could occur.

E is incorrect, because C is.

[ March 27, 2008: Message edited by: Jelle Klap ]



I am getting the output as
9a 9b
10a 10b

Please verify
Nikhil Raj
Greenhorn

Joined: Mar 20, 2008
Posts: 16
Originally posted by Avi Sridhar:



I am getting the output as
9a 9b
10a 10b

Please verify


The ID of the Thread is JVM dependent.
Anil Kinge
Greenhorn

Joined: Jan 07, 2008
Posts: 7
Can any one please tell me more about the threadID why it is started from 8.
David Grant
Greenhorn

Joined: Apr 01, 2008
Posts: 13
It isn't necessarily started from 8 - it depends on how many threads have been started by your JVM. The point of the question is to highlight that the format of the output is as follows:

xA xB yA yB

Where y > x

The actual numeric value of x and y will vary, but the pattern will not.
vijaya saradhi
Ranch Hand

Joined: Dec 31, 2007
Posts: 32
I am using jdk1.4 anyone please tell me if this getId() method is added in 1.5 version.

since my program does not compile if I use the above code as it is.
David Grant
Greenhorn

Joined: Apr 01, 2008
Posts: 13
Hi,

If you look in the JavaDoc for Thread.getId(), you'll notice a "Since" entry, when denotes which version of the JDK the method was added in. The entry for getId() in the Thread class indeed states 1.5, so that is the reason your application is failing to compile.

Cheers,

David
 
GeeCON Prague 2014
 
subject: thread question