Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Thread doubt please help

 
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Joining {
static Thread createThread(final int i, final Thread t1) {
Thread t2 = new Thread() {
public void run() {
System.out.println(i+1);
try {
t1.join();
} catch (InterruptedException e) {
}
System.out.println(i+2);
}
};
System.out.println(i+3);
t2.start();
System.out.println(i+4);
return t2;
}

public static void main(String[] args) {
createThread(10, createThread(20, Thread.currentThread()));
}
}

I executed the above program and got output as
---------- Java ----------
23
24
13
14
21
22
11
12
----------------------
But i am not understanding the code ?

please help me
 
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Karu,
I had posted a same question some time back. Had got an answer - could you search up for that post.

--
Shivani.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try these...

https://coderanch.com/t/245443/java-programmer-SCJP/certification/join-threads

https://coderanch.com/t/243927/java-programmer-SCJP/certification/join

https://coderanch.com/t/246361/java-programmer-SCJP/certification/Help-Required-code-threads
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, take the arguments to the createThread method:

1. The first time this method is called, the Thread argument that is passed to it is the mainthread.
2. The second time this method is called, the Thread argument that is passed is the Thread that was created in the first call.

Now, consider the thread stack:

1. The thread t2 that is created in the first call joins Thread t1 which is the mainthread.
2. The thread t2 that is created in the second call joins Thread t1, again, but this time this is the Thread created in the first call.

Let's give each thread a name. main thread, thread-first-call, thread-second-call.

This is the Thread Stack:

___________________________
|mainthread (i=20,i=10) |
|_________________________|
|thread-first-call (i=20) |
|_________________________|
|thread-second-call (i=10)|
|_________________________|

Top-down is the order in which the threads are eexecuted. thread-first-call will be executed after main thread finishes. thread-second-call will be executed after thread-first-call finishes.

Each thread has its own copy of i (indicated in brackets). mainthread executes and prints the value of (i+3) and (i+4) .. i.e 23 and 24 the first time. main thread executes again and prints the value of (i+3) and (i+4) .. i.e. 13 and 14. thread-first-call executes and prints the value of (i+1) and (i+2) .. 21 and 22. Last, thread-second-call executes and prints (i+1) and (i+2) .. i.e. 11 and 12.

NOTE:
1. To understand more clearly, print the give all threads a name and print them out along with (i+n)
2. Hope you got that run() method for the Thread t2 is defined in an anonymous inner class.
 
Karu Raj
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot to all of you .

do we except such type of questions in exam.

i am grate to all of you .

i appreciate you all for taking time to explian me .

I have one doubt
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Karthik Rajashekaran:
...do we except such type of questions in exam...


This is definitely more complex than what you should expect on the actual exam. It's good practice, because if you can work through this and understand what's going on, you're developing skills that will definitely help you on the exam. But you shouldn't need to spend this much time on a real question.
 
Karu Raj
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks weber a lot
 
A sonic boom would certainly ruin a giant souffle. But this tiny ad would protect it:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic