Thread and Join

K

KittyCreation

The following code produces this output always :
A.The number 14 is printed before number 22
B.The last number printed is 12.

Please can someone explain me the output of the following code.
Please explain how the code works to ensure the output behaviour...
I 've tried hours to understand it but I cannot.

//==============CODE

public class cc{
ArrayList arl = new ArrayList();
boolean flag = false;
static Thread create(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)
{
create(10,create(20,Thread.currentThread
())); }}
 
B

Boudewijn Dijkstra

KittyCreation said:
The following code produces this output always :
A.The number 14 is printed before number 22
B.The last number printed is 12.

Please can someone explain me the output of the following code.
Please explain how the code works to ensure the output behaviour...
I 've tried hours to understand it but I cannot.

No wonder, with such an unreadable layout...

Anyway, the order of events is as follows:
1. create(20, Thread.currentThread());
2. System.out.println(i + 3); --> 20 + 3 = 23
3. in no particular order: System.out.println(i + 1); and System.out.println(i
+ 4); --> 21 and 24
4. create(10, create(20, Thread.currentThread()));
5. System.out.println(i + 3); --> 10 + 3 = 13
6. in no particular order: System.out.println(i + 1); and System.out.println(i
+ 4); --> 11 and 14
7. the main thread dies, the thread created at (1) wakes up
8. System.out.println(i + 2); --> 20 + 2 = 22
9. the thread created at (1) dies, the thread created at (2) wakes up
10. System.out.println(i + 2); --> 10 + 2 = 12

//==============CODE

public class cc{
ArrayList arl = new ArrayList();
boolean flag = false;
static Thread create(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)
{
create(10,create(20,Thread.currentThread
())); }}
 
P

Patricia Shanahan

Boudewijn said:
No wonder, with such an unreadable layout...

Anyway, the order of events is as follows:
1. create(20, Thread.currentThread());
2. System.out.println(i + 3); --> 20 + 3 = 23
3. in no particular order: System.out.println(i + 1); and System.out.println(i
+ 4); --> 21 and 24
4. create(10, create(20, Thread.currentThread()));
5. System.out.println(i + 3); --> 10 + 3 = 13
6. in no particular order: System.out.println(i + 1); and System.out.println(i
+ 4); --> 11 and 14
7. the main thread dies, the thread created at (1) wakes up
8. System.out.println(i + 2); --> 20 + 2 = 22
9. the thread created at (1) dies, the thread created at (2) wakes up
10. System.out.println(i + 2); --> 10 + 2 = 12

I agree with this analysis.

Refactoring for clarity, rather than compactness, as well as formatting,
can help with understanding messy code. I haven't fixed the choices of
identifiers, because I wanted to keep the relationship to the existing code.

import java.util.ArrayList;

public class cc {
ArrayList arl = new ArrayList();

boolean flag = false;

static Thread create(final int i, final Thread t1) {
Thread t2 = new Thread(new T2(i,t1));
System.out.println(i + 3);
t2.start();
System.out.println(i + 4);
return t2;
}

static class T2 implements Runnable{
Thread t1;
int i;
T2(int i, Thread t1){
this.t1 = t1;
this.i = i;
}
public void run() {
System.out.println(i + 1);
try {
t1.join();
} catch (InterruptedException e) {
}
System.out.println(i + 2);
}
}

public static void main(String[] args) {
Thread thread0 = Thread.currentThread();
Thread thread1 = create(20,thread0);
Thread thread2 = create(10,thread1);
}
}
 
T

Thomas G. Marshall

Patricia Shanahan coughed up:
I agree with this analysis.

Refactoring for clarity,

Bah. Clarity and K&R brace placement is a conflict in terms.

:p''''''''

lol

rather than compactness, as well as
formatting, can help with understanding messy code. I haven't fixed
the choices of identifiers, because I wanted to keep the relationship
to the existing code.
import java.util.ArrayList;

public class cc {
ArrayList arl = new ArrayList();

boolean flag = false;

static Thread create(final int i, final Thread t1) {
Thread t2 = new Thread(new T2(i,t1));
System.out.println(i + 3);
t2.start();
System.out.println(i + 4);
return t2;
}

static class T2 implements Runnable{
Thread t1;
int i;
T2(int i, Thread t1){
this.t1 = t1;
this.i = i;
}
public void run() {
System.out.println(i + 1);
try {
t1.join();
} catch (InterruptedException e) {
}
System.out.println(i + 2);
}
}

public static void main(String[] args) {
Thread thread0 = Thread.currentThread();
Thread thread1 = create(20,thread0);
Thread thread2 = create(10,thread1);
}
}
 
J

jan V

Please can someone explain me the output of the following code.
Please explain how the code works to ensure the output behaviour...
I 've tried hours to understand it but I cannot.

Please rewrite your program to use qualtity (i.e. readable) identifiers.
Then repost on this group. No professional is going to donate/waste his/her
time analysing cryptic code like this. Sorry.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top