File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

thread question

 
Niala Nirell
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1951
7
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Niala Nirell
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much
[ March 27, 2008: Message edited by: Alain Sellerin ]
 
Sandeep Bhandari
Ranch Hand
Posts: 201
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
good question. Enjoyed cracking it.
 
Avi Sridhar
Ranch Hand
Posts: 48
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any one please tell me more about the threadID why it is started from 8.
 
David Grant
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic