John Brooks
John Brooks

Reputation: 57

sequential execution of threads in java

I have four threads t1,t2,t3,t4 acting on same object of the class.I have some restriction,t2 should execute only after t1 has executed and t3 should execute only after t2 has executed,and same is the case with t4.I mean sequence should compulsarily be t1 then t2 then t3 then t4.How can we make sure that these four threads will execute in sequence only.

Upvotes: 0

Views: 489

Answers (2)

Clinton Dsouza
Clinton Dsouza

Reputation: 328

trying using something known as a 'semaphore' .its basically a lock and key mechanism which will allow a single thread to run at a time provided they acquire a lock and release it when they are done.

example

Upvotes: 0

Kayaman
Kayaman

Reputation: 73528

If you need to control the sequence like that, you don't have parallelism, and multithreading is useless. You seem to need only a single thread that will perform those operations sequentially.

Upvotes: 6

Related Questions