georgeB96

JavaThread 2 Joint threads

Oct 29th, 2021
1,351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.98 KB | None | 0 0
  1.  
  2. public class Lab2 {
  3.    
  4.     public static void main(String[] args) {
  5.         Runnable joes = new SharingAResource("Eat at Joe's", 10, 2);
  6.         Runnable bills = new SharingAResource("Eat at Bill's", 10, 4);
  7.        
  8.         Thread j = new Thread(joes);
  9.         Thread b = new Thread(bills);
  10.        
  11.         j.start();
  12.         b.start();
  13.        
  14. //      try {
  15. //          j.join();
  16. //          b.join();
  17. //      } catch (InterruptedException e) {
  18. //          return;
  19. //      }
  20.        
  21.         System.out.println("Billboard closed");
  22.  
  23.        
  24.     }
  25.    
  26.    
  27.    
  28. }
  29.  
  30.  
  31. class SharingAResource implements Runnable {
  32.    
  33.     private String message;
  34.     private int counter;
  35.     private int pause;
  36.    
  37.     //constructor
  38.     public SharingAResource(String message, int counter, int pause){
  39.         this.message = message;
  40.         this.counter = counter;
  41.         this.pause = pause * 1000;
  42.     }
  43.    
  44.     public void run() {
  45.        
  46.         for(int i=0; i<counter; i++) {
  47.             try {
  48.                 Thread.sleep(this.pause);
  49.             } catch(InterruptedException e) {
  50.                 return;
  51.             }
  52.            
  53.             System.out.println(this.message);
  54.         }
  55.        
  56.     }
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment