Beruflich Dokumente
Kultur Dokumente
Java FAQ
Recent Topics
Register / Login
Author
Hi, I am new to java concurant programming and trying out with few examples.I wanted to start 5 threads and stop them by printing (System.out.println) Thread1 started,Thread2Started,Thread3Started etccc then Thread1stopped,Thread1stopped etccc.. I have writen the below program.Please help tp change it to produce all started messages once then stopped messages. public class SimpleThread { public static void main (String[] args) { for(int i=0;i<5;i++) new RunnableTask(i).start(); } } class RunnableTask extends Thread { RunnableTask(int i) { super(String.valueOf(i)); } public void run() { System.out.println("Thread "+this.getName()+"Started"); System.out.println("Thread "+this.getName()+"Stopped"); } }
Regards Radha
Well, this might not be as simple as you had hoped. Threads tend to execute in their own order unless you force order upon them using various synchronization tools. To get an idea of how synchronization tools work, I suggest reading the Java Concurrency Tutorial. For your application, this task becomes relatively simple using one of the new(ish) locking mechanisms from the java.util.concurrent package. Basically, you have 5 tasks, and want to make sure they all reach a certain point (after the 'started' message) before they can continue. This is the perfect use for the CountDownLatch. An example:
6
I like...
www.coderanch.com/t/441934/threads/java/Starting-threads
1/3
7/14/13
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 . 2 2 . 2 3 . 2 4 . 2 5 . 2 6 . 2 7 . 2 8 . 2 9 . 3 0 . 3 1 . 3 2 . 3 3 . 3 4 . 3 5 . 3 6 . 3 7 . 3 8 . 3 9 . 4 0 . 4 1 . 4 2 . 4 3 . 4 4 . 4 5 . 4 6 .
p a c k a g ec o n c u r r e n c y ; i m p o r tj a v a . u t i l . c o n c u r r e n t . C o u n t D o w n L a t c h ; p u b l i cc l a s sC o u n t D o w n { p u b l i cs t a t i cv o i dm a i n ( S t r i n g [ ]a r g s ) { f i n a li n tt h r e a d C o u n t=5 ; C o u n t D o w n L a t c hc d l=n e wC o u n t D o w n L a t c h ( t h r e a d C o u n t ) ; f o r( i n ti=1 ;i< =t h r e a d C o u n t ;i + + ) { n e wR u n n a b l e T a s k ( c d l ,i ) . s t a r t ( ) ; } } s t a t i cc l a s sR u n n a b l e T a s ke x t e n d sT h r e a d { p r i v a t ef i n a lC o u n t D o w n L a t c hl a t c h ; p r i v a t ef i n a li n ti d ; R u n n a b l e T a s k ( C o u n t D o w n L a t c hc d l ,i n ti ) { t h i s . l a t c h=c d l ; t h i s . i d=i ; } p u b l i cv o i dr u n ( ) { S y s t e m . o u t . p r i n t l n ( " T h r e a d" + i d + "s t a r t e d . " ) ; t r y { l a t c h . c o u n t D o w n ( ) ;/ / l e to t h e rt h r e a d sk n o wIr e a c h e dh e r e l a t c h . a w a i t ( ) ;/ / W a i tu n t i la l lt h r e a d sr e a c h e dh e r e } c a t c h( I n t e r r u p t e d E x c e p t i o ni e ) { i e . p r i n t S t a c k T r a c e ( ) ; } S y s t e m . o u t . p r i n t l n ( " T h r e a d"+i d+"e n d e d . " ) ; } } }
Just as an FYI, it is normally frowned upon to extend Thread, unless you absolutely need to. Instead, implement the Runnable interface and pass it to a new thread:
view plain c opy to c lipboard print ?
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 .
n e wT h r e a d ( n e wR u n n a b l e T a s k ( c d l ,i ) ) . s t a r t ( ) ;/ * i n s t e a do fn e wR u n n a b l e T a s k ( c d l ,i ) . s t a r t ( ) ;* / . . . s t a t i cc l a s sR u n n a b l e T a s ki m p l e m e n t sR u n n a b l e / * n o te x t e n d sT h r e a d * / { . . .
Steve
www.coderanch.com/t/441934/threads/java/Starting-threads
2/3
7/14/13
www.coderanch.com/t/441934/threads/java/Starting-threads
3/3