Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2005

Tip: Looking for answers? Try searching our database.

Thread and Join

Thread view: 
KittyCreation - 30 Jul 2005 06:53 GMT
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
         ()));    }}
Boudewijn Dijkstra - 30 Jul 2005 13:26 GMT
> The following code produces this output always :
> A.The number 14 is printed before number 22
[quoted text clipped - 3 lines]
> 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
>
[quoted text clipped - 22 lines]
>       create(10,create(20,Thread.currentThread
>          ())); }}
Patricia Shanahan - 30 Jul 2005 14:27 GMT
>>The following code produces this output always :
>>A.The number 14 is printed before number 22
[quoted text clipped - 19 lines]
> 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);
  }
}
Thomas G. Marshall - 30 Jul 2005 16:34 GMT
Patricia Shanahan coughed up:

>>> The following code produces this output always :
>>> A.The number 14 is printed before number 22
[quoted text clipped - 23 lines]
>
> 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
[quoted text clipped - 38 lines]
>   }
> }

Signature

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".

jan V - 30 Jul 2005 17:16 GMT
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.