Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how does getId() gives value to thread

 
Ademola Okerinde
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ademola Okerinde
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 394
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic