872 Multithreading Chapter 15 35 (Yahoo web hosting) 36 // create

872 Multithreading Chapter 15 35 36 // create GUI components, register listeners and attach 37 // components to content pane 38 for ( int count = 0; count < SIZE; count++ ) { 39 outputs[ count ] = new JLabel(); 40 outputs[ count ].setBackground( Color.green ); 41 outputs[ count ].setOpaque( true ); 42 container.add( outputs[ count ] ); 43 44 checkboxes[ count ] = new JCheckBox( "Suspended" ); 45 checkboxes[ count ].addActionListener( this ); 46 container.add( checkboxes[ count ] ); 47 } 48 } 49 50 // Create and start threads. This method called after init 51 // and when user revists Web page containing this applet 52 public void start() 53 { 54 // create threads and start every time start is called 55 for ( int count = 0; count < threads.length; count++ ) { 56 57 // create Thread and initialize it with object that 58 // implements Runnable 59 threads[ count ] = new Thread( new RunnableObject(), 60 "Thread " + ( count + 1 ) ); 61 62 // begin executing Thread 63 threads[ count ].start(); 64 } 65 } 66 67 // determine thread location in threads array 68 private int getIndex( Thread current ) 69 { 70 for ( int count = 0; count < threads.length; count++ ) 71 72 if ( current == threads[ count ] ) 73 return count; 74 75 return -1; 76 } 77 78 // called when user switches Web pages; stops all threads 79 public synchronized void stop() 80 { 81 // Indicate that each thread should terminate. Setting 82 // these references to null causes each thread's run 83 // method to complete execution. 84 for ( int count = 0; count < threads.length; count++ ) 85 threads[ count ] = null; 86 Fig. 15.17 Demonstrating the Runnableinterface, suspending threads and resuming threads (part 2 of 5).
Please visit Domain Name Hosting services for high quality webhost to host and run your jsp applications.

Leave a Reply