Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Thread and Join

Reply
Thread Tools

Thread and Join

 
 
KittyCreation
Guest
Posts: n/a
 
      07-30-2005
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
())); }}

 
Reply With Quote
 
 
 
 
Boudewijn Dijkstra
Guest
Posts: n/a
 
      07-30-2005
"KittyCreation" <(E-Mail Removed)> schreef in bericht
news:(E-Mail Removed) oups.com...
> 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
> ())); }}
>



 
Reply With Quote
 
 
 
 
Patricia Shanahan
Guest
Posts: n/a
 
      07-30-2005
Boudewijn Dijkstra wrote:
> "KittyCreation" <(E-Mail Removed)> schreef in bericht
> news:(E-Mail Removed) oups.com...
>
>>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
>


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);
}
}
 
Reply With Quote
 
Thomas G. Marshall
Guest
Posts: n/a
 
      07-30-2005
Patricia Shanahan coughed up:
> Boudewijn Dijkstra wrote:
>> "KittyCreation" <(E-Mail Removed)> schreef in bericht
>> news:(E-Mail Removed) oups.com...
>>
>>> 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
>>

>
> I agree with this analysis.
>
> Refactoring for clarity,


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

''''''''

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);
> }
> }




--
If I can ever figure out how, I hope that someday I'll
succeed in my lifetime goal of creating a signature
that ends with the word "blarphoogy".


 
Reply With Quote
 
jan V
Guest
Posts: n/a
 
      07-30-2005
> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Join a thread and get the return value of a function Lie Ryan Python 2 01-13-2010 08:03 PM
Timeouts with Thread#join and Net:HTTP Rushi Ruby 2 03-20-2009 12:15 AM
You must join New York Paid To Read !!!!!!! NewYork PTR is online!!! JOIN NOW!!!!!! Alan Silver ASP .Net 0 06-05-2006 03:27 PM
list.join()... re.join()...? Do they exist? (newbie questions...) googleboy Python 1 10-01-2005 12:56 PM
IllegalThreadStateException and Thread.join googlepost Java 2 07-22-2003 07:22 PM



Advertisments