1 Replies - 250 Views - Last Post: 01 August 2013 - 02:22 PM Rate Topic: -----

#1 Java1981  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 18-May 13

Threads

Posted 01 August 2013 - 02:07 PM

I am trying to create and run 5 threads simultaneously

I am trying to get it to go to sleep for randomly selected time in the range of 50-500.
I can get 0-500 but unsure how to set it from 50-500.

Also I am trying to repeat each thread 10 times. I tried a loop but I am not sure where to put it or what variable to use.

Any help would be appreciated.

Here is my code so far. I have 2 classes


import java.util.Random;

/**
 *
 * @author 
 */
public class PrintTask implements Runnable {
   
   private final int sleepTime;
   private final String taskName; 
   private final static Random generator = new Random();
   
   public PrintTask (String name){
       taskName = name;
       sleepTime = generator.nextInt(500);
       
   }
    
   public void run()
           
   {
   
       try
           
       {
           System.out.printf("%s \n",taskName);
           
           System.out.printf("%s going to sleep for %d milliseconds. \n",
                   taskName, sleepTime);
           Thread.sleep(sleepTime);
       }
       catch (InterruptedException exception){
           System.out.printf("%s %s\n", taskName,
           "terminated prematurely due to interruprion");     
       }
       System.out.printf("%s done sleeping\n",taskName);
       
   }
      
       }

 
       
       
   
   
   









My Main



import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 *
 * @author 
 */
public class TaskExecutor {
   
    public static void main (String[]args)
    {
          

//          
//
//        for (int count = 1; count <= 10; count++) {
//            
//            
//               
//                
//                System.out.printf("\t\t\t%2d\n", );
//            } 
        
        PrintTask task1 = new PrintTask("task1");
        PrintTask task2 = new PrintTask("task2");
        PrintTask task3 = new PrintTask("task3");
        PrintTask task4 = new PrintTask("task4");
        PrintTask task5 = new PrintTask("task5");
        
      //  System.out.println("Starting Executor");
        
        ExecutorService threadExecutor = Executors.newCachedThreadPool();
        
        threadExecutor.execute (task1);  
        threadExecutor.execute (task2);  
        threadExecutor.execute (task3);  
        threadExecutor.execute (task4);  
        threadExecutor.execute (task5);  
        
        threadExecutor.shutdown();
        
        System.out.println("This is main Program,  \n");
        System.out.println("Threads being created, \n");
        System.out.println("Threads started,  \n");
        System.out.println("Main Ends, \n");
        
    
    }
}





Is This A Good Question/Topic? 0
  • +

Replies To: Threads

#2 farrell2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 841
  • View blog
  • Posts: 2,576
  • Joined: 29-July 11

Re: Threads

Posted 01 August 2013 - 02:22 PM

View PostJava1981, on 01 August 2013 - 09:07 PM, said:

I am trying to get it to go to sleep for randomly selected time in the range of 50-500.
I can get 0-500 but unsure how to set it from 50-500.


Think. What would adding 50 give you?

rand.nextInt(501) + 50;



This gives an int from 50 to 550. Remember that nextInt() gives a number between 0 and your argument, exclusive. You need to make it 501 to overcome that.

This post has been edited by farrell2k: 01 August 2013 - 02:23 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1