2

my task is to create thread in this order: if A start->start B and C, if B start->start D. And destroy them in reverse order If D then B. If B and C then A. I hope you get it. I manage to do it but I guess there is better way to do it. Do you have any suggestions?

After your comments i have changed my code and it is much more simply. But now it looks "stupid". I would like to change hardcore of if statements and implementation, any advice? tnx for advice I'm learning with you.

This is my new code:

 import java.util.*; class RobotController implements Runnable{ String name; public void run() { Thread t = Thread.currentThread(); System.out.println(t.getName() + " status = " + t.isAlive()); System.out.println(t.getName() + " status = " + t.getState()); } public static void main(String args[]) throws InterruptedException{ Thread thread_A = new Thread(new RobotController(), "Thread A"); Thread thread_B = new Thread(new RobotController(), "Thread B"); Thread thread_C = new Thread(new RobotController(), "Thread C"); Thread thread_D = new Thread(new RobotController(), "Thread D"); thread_A.start(); thread_A.join(); System.out.println(thread_A.getState()); thread_B.start(); thread_B.join(); System.out.println(thread_B.getState()); thread_C.start(); thread_C.join(); System.out.println(thread_C.getState()); thread_D.start(); System.out.println(thread_D.getState()); } } 
7
  • Sounds like you should define a task (Runnable), with a parent field of the same type, and boolean flag to kill it. Commented Jun 12, 2013 at 18:12
  • you synchronize on method's local variable, that's pointless. Commented Jun 12, 2013 at 18:32
  • if A start->start B and C by this u mean after A completes its work then B start in sequence..and after B completes its work then C start ? Commented Jun 12, 2013 at 18:44
  • And, actually, order in which threads are started is completely unimportant. What is important is the order in which threads read/modify some data. You should clarify if there some data which must be accessed by several threads sequentially. Commented Jun 12, 2013 at 18:44
  • @VishalK no i need to start them in order A-B,C-D and finished them in reverse order. So thread A is starting first and finishing last Commented Jun 12, 2013 at 21:54

4 Answers 4

3

There are some flaws in your code which will make it not to work accordingly sometimes:

  1. You called thread_A.start() and then checked thread_A.isAlive(). Now what if , thread_A is already completed before thread_A.isAlive() condition is checked?.thread_B and thread_C is never started. Your application fails.
  2. Assume that thread_A is not completed and thread_A.isAlive() condition is passed, then starting of thread_B before thread_C is not always guaranteed by Java thread scheduler. Again your application fails.
  3. Assume that thread_B starts before thread_C and if thread_B completes before thread_B.isAlive() is checked then the if condition fails and thread_D is never started. Again your application fails.

Now a point to ponder:
There is no need to check if the thread is alive after its join() method is called. It is an unnecessary runtime overhead.

EDIT
OK, Here is the modified version of code..I hope it would let you understand the dynamics of thread:

class RobotController implements Runnable { private final Object lock = new Object(); private void notifyThread() { synchronized(lock) { lock.notify(); } } public void run() { synchronized(lock) { try { System.out.println(Thread.currentThread().getName() + " started"); lock.wait(); System.out.println(Thread.currentThread().getName()+ " stopped"); } catch (InterruptedException ex) { ex.printStackTrace(); } } } public static void main(String args[]) throws InterruptedException { RobotController rca = new RobotController(); RobotController rcb = new RobotController(); RobotController rcc = new RobotController(); RobotController rcd = new RobotController(); Thread thread_A = new Thread(rca,"Thread A"); Thread thread_B = new Thread(rcb,"Thread B"); Thread thread_C = new Thread(rcc,"Thread C"); Thread thread_D = new Thread(rcd,"Thread D"); thread_A.start(); while (thread_A.getState() != Thread.State.WAITING) { Thread.sleep(100); } thread_B.start(); thread_C.start(); while (thread_B.getState() != Thread.State.WAITING && thread_C.getState() != Thread.State.WAITING) { Thread.sleep(100); } thread_D.start(); while (thread_D.getState() != Thread.State.WAITING) { Thread.sleep(100); } rcd.notifyThread(); thread_D.join(); rcc.notifyThread(); thread_C.join(); rcb.notifyThread(); thread_B.join(); rca.notifyThread(); } } 

And here is the output:

Thread A started Thread B started Thread C started Thread D started Thread D stopped Thread C stopped Thread B stopped Thread A stopped 
Sign up to request clarification or add additional context in comments.

2 Comments

tnx for helping me. Now i changed my code according to @Darshan Mehta but i still don't know how to solve problem. I dont know how and where should i put this conditions. Do you have any idea?Ps: i update my new code
Tnx so much, i understand it now much better.
2

In multi threading, there is no need of synchronization unless the common data is shared by multiple threads. In your case, you want to start and stop the threads in a particular order. For this, there is join method in Thread class. This link shows good example of join method.

5 Comments

join won't allow concurrent execution of threads, only sequential
Yes. But the execution sequence required here can only be achieved by join. May be we can place join call in the last line of thread execution code to achieve maximum concurrency.
Now, after review OP code, I think my comment is irrelevant )
@DarshanMehta i did it like you sad and now my program looks so much simple and it is still working. But now my code looks stupid :P I would like to change hardcore of if statements and implementation, any advice? tnx for advice I'm learning with you. PS: i uploaded how my code looks now
Provided my answer to this comment as a separate answer as it was too long.
0

In my opinion, it is quite strange to use synchronized (lock) in your run method to lock your object. The reason is that in each Thread object has the different lock attribute, which is belong to each object. It means you are trying to lock the different objects. Actually, it doesn't make sense.

Basically, the object that you should apply the synchronized are any shared objects. For example, you need to count something and then you create a class object to share it in your class. In this case, it should be locked while being read or written.

3 Comments

The lock is shared between run() and stop_do(). The synchronization is needed to call wait/notify.
@fgb Of course, but it doesn't quite make sense to lock the object attributes instead of shared object.
@Maverick it seams that i just didnt got that. Do you have some example to show me this in practice?
0

I would like to highlight two points here:

  1. Have a look at thread execution life cycle here. It says that, when start() method is called, thread enters in runnable state and not in running state. When thread enters in running state, that means run() method is getting exexuted. CPU/OS decides the priority of which thread should be transferred from runnable to running thread. E.g. if you call start() method for 4 threads, it is not necessary that they will execute in that particular order. (Running the same program in my pc multiple times gives me different outputs.

In your case, when the condition if(thread_A.isAlive()) is executed, it is possible that the thread A may not be in running state. Hence, the control will not go into if which is not correct. To correct this behavior, in main, a while loop should implemented which waits until the thread becomes alive and so on.

2 . In your program, you have not assigned names to the threads and you are printing the name in the run() method. In this case, JVM assigns the names to threads in order of their execution e.g. first thread to execute will have name as 'Thread-0' and so on. Hence, we will not be able to identify which thread executed first. Assign the names using setName() method.

1 Comment

tnx for helping me. I learned more about threads. Now i fixed my program and threads are starting in order A-B-C-D but after join method they are finished, how can i fix this? I tried to find it on internet but somehow i didnt find nothing useful. Ps: i updated again my code

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.