Advertisement
Guest User

Untitled

a guest
May 4th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.87 KB | None | 0 0
  1. package pokusaj1;
  2.  
  3. public class pokusaj1 {
  4.    
  5.     public static Thread t1;
  6.     public static Thread t2;
  7.    
  8.     public static volatile boolean flag1 = false;
  9.     public static volatile boolean flag2 = false;
  10.    
  11.     public static void createThreads(){
  12.         Runnable proces1 = new Runnable() {
  13.            
  14.             @Override
  15.             public void run() {
  16.                 while(true){
  17.                     flag1 = true;
  18.                     while (flag2 == true);
  19.                     System.out.println("Proces1");
  20.                     flag1 = false;
  21.                 }
  22.             }
  23.         };
  24.        
  25.         Runnable proces2 = new Runnable() {
  26.            
  27.             @Override
  28.             public void run() {
  29.                 while(true){
  30.                     flag2 = true;
  31.                     while(flag1 == true);
  32.                     System.out.println("Proces2");
  33.                     flag2 = false;
  34.                    
  35.                 }
  36.                
  37.             }
  38.         };
  39.        
  40.         t1 = new Thread(proces1);
  41.         t2 = new Thread(proces2);
  42.        
  43.         t1.start(); t2.start();
  44.        
  45.        
  46.     }
  47.    
  48.     public static void main(String[] args){
  49.         createThreads();
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement