File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how does getId() gives value to thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "how does getId() gives value to thread" Watch "how does getId() gives value to thread" New topic
Author

how does getId() gives value to thread

Ademola Okerinde
Greenhorn

Joined: Jun 27, 2011
Posts: 11
please help analyse the return value from getId()

6) Given:
5. public class Lockdown implements Runnable {
6. public static void main(String[] args) {
7. new Thread(new Lockdown())start();
8. new Thread(new Lockdown())start();
9. }
10. public void run() { locked(Thread.currentThread()getId()); }
11. synchronized void locked(long id) {
12. System.out.print(id + "a ");
13. System.out.print(id + "b ");
14. }
15. }

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. (*)
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.

REFERENCE:
JLS 3.0
Option B is correct. Two different Lockdown objects are using the locked() method
Aj Jaro
Greenhorn

Joined: Oct 24, 2010
Posts: 9
Per the API the getId():

Returns the identifier of this Thread. The thread ID is a positive long number generated when this thread was created. The thread ID is unique and remains unchanged during its lifetime. When a thread is terminated, this thread ID may be reused.


The numbers they use in these examples are totally arbitrary, however they are still useful. Because the thread will maintain the same ID throughout its life it will help you predict some of the output. The reason the answer could be "6a 7a 6b 7b" is because on lines 6 and 7 new Lockdown objects were created so locked() can be accessed by multiple threads at the same time and therefore the synchronized code is unnecessary in this scenario.


OCPJP 6
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Hello Ademola...getId() does NOT give values to thread, it is the JVM that gives values to threads in other to facilitate their identification. In lines 6 and 7 of the program two threads are instantiated and the JVM assings different id's to both threads, the getId() method simply returns the id's that were assigned by the JVM.

HTH

Regards

Ikpefua

P.S. check your private message box.


OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Ademola Okerinde
Greenhorn

Joined: Jun 27, 2011
Posts: 11
Ikpefua Jacob-Obinyan wrote:Hello Ademola...getId() does NOT give values to thread, it is the JVM that gives values to threads in other to facilitate their identification. In lines 6 and 7 of the program two threads are instantiated and the JVM assings different id's to both threads, the getId() method simply returns the id's that were assigned by the JVM.

HTH

Regards

Ikpefua

P.S. check your private message box.

many many thanks to you?
Actually have booked for the exam for early August. i have tried many practice Tests. i am currently trying Examlab but my scores have not been really okay
Diagnostic - 29%
Practice Test 1 - 31%
Pr. Test 2 - 38%
will be writing Test 3 tomorrow.
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Ademola Okerinde wrote:many many thanks to you?
Actually have booked for the exam for early August. i have tried many practice Tests. i am currently trying Examlab but my scores have not been really okay
Diagnostic - 29%
Practice Test 1 - 31%
Pr. Test 2 - 38%
will be writing Test 3 tomorrow.

Its my pleasure bro... Dont worry about examLab scores, they are a WARNING that the real exams arent a childs play neither... Be informed that examLab questions are more difficult than the real exams. My highest score in examLab was 52%. It is a VERY good brain stimulator towards the real exams, by improving your capacity to analyse codes in different ways. REMEMBER to also take the other mock exams I mentioned to you because of their "Similarity" to the real exams.

HtH

Ikpefua.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how does getId() gives value to thread
 
Similar Threads
thread question
Sun Sample Question
Multithreading: Two errors with education.oracle.com sample exam question
synchronized
Thread Question from Sun's Free Mock Test