Java threads - communication between threads

Communication between multiple threads in Java is how to achieve?

Thread communication way:

(1) shared variables

  Communication between threads can send a signal, a simple method of transmitting a signal is provided which re-Shared signal value. A boolean type thread provided in a member variable hasDataToProcess sync block is true, the thread B reads the value hasDataProcess sync blocks, as follows:

Package com.itheima.threadTest; 

public  class ThreadtTest {
     // shared variable 
    Private  Boolean hasDataToProcess = to false ;
     // value 
    public  Boolean getHasDataToProcess () {
         return hasDataToProcess; 
    } 
    // set values 
    public  void setHasDataToProcess ( Boolean hasDataToProcess) {
         the this . = hasDataToProcess hasDataToProcess; 
    } 
        
    public  static  void main (String [] args) {
         // create an object
        Final ThreadtTest = My new new ThreadtTest ();
         // Thread 1 hasDataToProcess value set to true 
        Final the Thread = T1 new new the Thread ( new new the Runnable () { 
            @Override 
            public  void RUN () { 
                my.setHasDataToProcess ( to true ); 
                
            } 
        }); 
        
        // thread 1 starts executing 
        t1.start (); 
        
        // thread 2 value 
        the thread T2 = new new the thread ( new new the Runnable () { 
            @Override 
            public  void RUN () {
                 the try{
                     // wait for a thread executing the 
                    t1.join (); 
                } the catch (Exception E) { 
                    e.printStackTrace (); 
                } 
                
                my.getHasDataToProcess (); 
                
                System.out.println ( "t1 changes the value:" + My .getHasDataToProcess ()); 
            } 
        }); 
        t2.start (); 
    }

 

 

(2) wait / notify mechanism

  Creator consumer cases

Package com.itheima.threadTest; 

// resource 
class the Resource {
     Private String name;
     Private  int COUNT =. 1 ;
     // Flag 
    Private  Boolean In Flag = false ;   // When the flag is false when the producer is executed, as is true consumers execution 
    
    // generate resources 
    public  the synchronized  void the SET (String name) {
         // according to their own flag to determine whether the implementation of 
        the while (flag) {
             // when the flag is true, the producers wait for consumers to perform 
            the try {
                 / / Note if it is synchronized block, then we would use that object locked to call wait ()
                the wait (); 
            } the catch (InterruptedException E) { 
                e.printStackTrace (); 
            } 
        } 
        
        // if it executes the to 
        the this .name = name + "---" COUNT + ++ ; 
        System.out.println (Thread.currentThread () .getName () + "producer .... ......." + the this .name); 
        
        // modify flag 
        in flag = to true ;
         // wake of the other threads wait for 
        the this .notifyAll (); 
    } 
    
    / / consumer resources 
    public  the synchronized  void OUT () {
         // judgment flag 
        the while (!flag) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        
        System.out.println(Thread.currentThread().getName()+".....消费者......."+this.name);
        
        flag=false;
        this.notifyAll();
    }
        
}

//生产者线程
class Producer implements Runnable{
    private Resource res;
    Producer(Resource res){
        this.res=res;
    } 
    // creator generates a resource 
    @Override
     public  void RUN () {
         for ( int I = 0; I <. 5; I ++ ) { 
            res.set ( "commodity!" ); 
        } 
        
    }     
} 

// consumer spending resources 
class Consumer the implements the Runnable {
     Private the Resource Resource;
     public Consumer (the Resource Resource) {
         the this .resource = Resource; 
    } 
    @Override 
    public  void RUN () {
         for ( int I = 0; I <. 5; I ++) { 
            Resource.out (); 
        } 
        
    } 
} 

public  class ProducerConsumerDemo {
     public  static  void main (String [] args) {
         // participate in a common resource 
        the Resource = R & lt new new the Resource (); 
        
        // two threads 
        Producer Producer = new new Producer (R & lt); 
        Consumer Consumer = new new Consumer (R & lt); 
        the Thread T1 = new new the Thread (Producer); 
        the Thread T2 = new new the Thread (Consumer); 
        
        t1.start();
        t2.start (); 
    }
}

  operation result:

     

 

Guess you like

Origin www.cnblogs.com/zhilili/p/11984757.html
Recommended