Thread and Join

Discussion in 'Java' started by KittyCreation, Jul 30, 2005.

  1. 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
    ())); }}
    KittyCreation, Jul 30, 2005
    #1
    1. Advertising

  2. "KittyCreation" <> schreef in bericht
    news:...
    > 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
    > ())); }}
    >
    Boudewijn Dijkstra, Jul 30, 2005
    #2
    1. Advertising

  3. Boudewijn Dijkstra wrote:
    > "KittyCreation" <> schreef in bericht
    > news:...
    >
    >>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);
    }
    }
    Patricia Shanahan, Jul 30, 2005
    #3
  4. Patricia Shanahan coughed up:
    > Boudewijn Dijkstra wrote:
    >> "KittyCreation" <> schreef in bericht
    >> news:...
    >>
    >>> 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.

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




    --
    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".
    Thomas G. Marshall, Jul 30, 2005
    #4
  5. KittyCreation

    jan V Guest

    > 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.
    jan V, Jul 30, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. googlepost
    Replies:
    2
    Views:
    1,570
    Roedy Green
    Jul 22, 2003
  2. Alan Silver
    Replies:
    0
    Views:
    877
    Alan Silver
    Jun 5, 2006
  3. googleboy
    Replies:
    1
    Views:
    918
    Benji York
    Oct 1, 2005
  4. Lie Ryan
    Replies:
    2
    Views:
    411
  5. Rushi
    Replies:
    2
    Views:
    302
    7stud --
    Mar 20, 2009
Loading...

Share This Page