Thread Cycle question

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
zhengye1
Posts: 17
Joined: Wed Jan 07, 2015 12:06 am
Contact:

Thread Cycle question

Post by zhengye1 »

I hope I put my question into right section, because this question is on my actual exam today

Question:
Given the following code

Code: Select all

class MyThread extends Thread{
    String name;
    public MyThread(String name){
        this.name = name;
    }
    
    public void run(){
        System.out.println(this.name);
    }
}

public class Test{

     public static void main(String []args){
        MyThread t1 = new MyThread("MyThread 1");
        MyThread t2 = new MyThread("MyThread 2");
        try{
            t1.start();
            t1.join();
            t2.start();
            t2.join();
            t1.start();
        }catch(Exception e){
            e.printStackTrace();
        }
     }
}
Choose best answer for following:
A) not compile;
B)
MyThread 1
MyThread 2
MyThread 1
C) it will throws Runtime Exception

Originally I select B, because I think the cycle of thread, t1 will died due to t1.join, I can start the t1 again.

But the actual output I got is C), which is following:
MyThread 1
MyThread 2
java.lang.IllegalThreadStateException
at java.lang.Thread.start(Thread.java:705)
at HelloWorld.main(HelloWorld.java:22)
Do I misunderstanding about Thread.join()?

admin
Site Admin
Posts: 8944
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Thread Cycle question

Post by admin »

No, your understanding about join is correct. But you overlooked the fact that you cannot restart at thread that has already been started. Notice that the last call to t1.start will throw an exception because t1 was already started (and in fact already finished as well).

BTW, do you mean you took the real exam today and got this question?
Did you pass? Please share your experience!

-Paul.
If you like our products and services, please help us by posting your review here.

zhengye1
Posts: 17
Joined: Wed Jan 07, 2015 12:06 am
Contact:

Re: Thread Cycle question

Post by zhengye1 »

Yes, I took the real exam today, most question are silimar to the software, but I think I overlook some of the objective, that is why I failed with 58% T.T

Btw, I actually remeber some of the question and I want to share, where should I put these question in this forum?

admin
Site Admin
Posts: 8944
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Thread Cycle question

Post by admin »

Very sorry to hear that. You can send the questions to us by email to support @ enthuware . com.
If you like our products and services, please help us by posting your review here.

zhengye1
Posts: 17
Joined: Wed Jan 07, 2015 12:06 am
Contact:

Re: Thread Cycle question

Post by zhengye1 »

Already email the questions, hopefully you see it

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests