Results 1 to 2 of 2
  1. #1
    Saran185 is offline Member
    Join Date
    Jan 2011
    Posts
    36
    Rep Power
    0

    Default make thread to pause execution

    hi..
    I want to pause the execution of a thread. i tried thread.wait/.suspend()..but could not pause execution. basically i am developing a media player in java. when the audio plays simultaenously a text file content wil be displyed on screen. now when i pause the audio by clicking pause button the display of text should also be paused . i am using 2 threads. 1 st for playing audio. 2nd for displaying text. i have attached my code .when i run iam not able to pause the display of text. kindly help..
    the below code is for displaying text from a file. this is placed in a function named display() .
    Java Code:
     Runnable r2 = new Runnable() 
    				 {
    		         public void run() 
    				  {
    		           try 
    				    { 
    		             
    					  
    					   
                          for(int j=0;j<r.length;j++)                      
    		              { textArea1.append(r[j]);                                 // highliting the words
    		              textArea1.append(" ");
    		              textArea1.setForeground(Color.red);
    					  textArea1.setLineWrap(true);
    					  int t=dur/nbWords; 
    		     		  Thread.currentThread().sleep(t);
    					  dur=dur-t;
    					  nbWords--;
    		              
    		              }          
    	                }
    					catch (InterruptedException x) 
    					            {
    	                      x.printStackTrace();
                                      }
    		           }             
    		         };
    		    tr2 = new Thread(r2);                                   // thread 2 for highlight
    		          tr2.start();
    below code is the pausing event.
    it must make the tr2 thread to pause execution.
    Java Code:
      public void controllerUpdate (ControllerEvent e)
       {
     if (e instanceof StopByRequestEvent)
          {
              System.out.println("stop");
    		  
    		
                    try 
    				{ 			  
                 		System.out.println("1");
    
    				 tr2.sleep(5000);
    			     //Thread.currentThread().sleep(5000);
    				  System.out.println("2");
    				}
                    catch (Exception e1) { System.out.println(e1); }
              
    		// Thread.currentThread().sleep(5000);
    		        
    		  
              return;
          }
    }
    it prints 2 after every 5 secs. but the text display is not getting paused. the text is getting displayed continuously without the pause time.
    Last edited by Saran185; 03-14-2011 at 10:30 AM.

  2. #2
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Smile

    System.out.println("2");
    Put this line in a conditional block, may be a flag is enough. Set/reset this flag on your event.

Similar Threads

  1. thread execution one after another
    By turanan in forum New To Java
    Replies: 16
    Last Post: 05-08-2012, 01:11 PM
  2. Thread Execution
    By sunilragidi in forum Threads and Synchronization
    Replies: 2
    Last Post: 12-07-2010, 04:38 PM
  3. Replies: 10
    Last Post: 11-29-2010, 03:02 PM
  4. Make animation pause
    By mneskovic in forum New To Java
    Replies: 11
    Last Post: 06-08-2010, 06:16 PM
  5. Make Java Pause
    By FlyNn in forum New To Java
    Replies: 2
    Last Post: 02-10-2010, 11:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •