Java, how to achieve multi-threaded communication

Thread communication way:

  1, shared variables

      Inter-thread communication by sending a signal, a transmission signal is a simple way to set signal values of the variable in a shared object. A thread is provided in a member variable boolean type sync blocks read in hasDataToProcess hasDataToProcess member variable is true, the thread B are in the synchronized block. This simple example using an object that holds a signal, and provides a set and get methods.

  

public  class MySignal1 {
     // shared variable 
    Private Boolean = hasDataToProcess to false ;

    //取值
    public boolean getHasDataProcess() {
        return hasDataToProcess;
    }

    //存值
    public void setHasDataToProcess(boolean hasDataToProcess) {
        this.hasDataToProcess = hasDataToProcess;
    }

    public  static  void main (String [] args) {
         // the same object 
        Final MySignal1 = My new new MySignal1 ();
         // thread 1 is disposed hasDataToProcess to true 
        Final the Thread = T1 new new the Thread ( new new the Runnable () {
            @Override
            public void run() {
                my.setHasDataToProcess(true);
            }
        });
        t1.start();
        // thread takes the value 2 hasDataToProcess 
        the Thread T2 = new new the Thread ( new new the Runnable () {
            @Override
            public  void RUN () {
                 the try {
                     // Wait for a complete thread values 
                    t1.join ();
                } catch (InterruptedException e) {
                    e.printStackTrace ();
                }
                my.getHasDataProcess();
                . The System OUT .println ( " after changing the value of t1: " + my.getHasDataProcess ());
            }
        });
        t2.start();
    }
}

Results are as follows:

  

After changing the value of t1: to true

 

  2. Wait / Wake (wait / notify) mechanism

     To resources, for example, producers produce a resource to inform consumers consume a resource, producers continue to produce resources, consumer spending resources, this cycle, as follows.

import sun.security.util.Password;

//资源类
class Resource {
    private String name;
    private int count = 1;
    private boolean flag = false;

    public  the synchronized  void the SET (String name) {
         // productive resources 
        the while (Flag) {
             the try {
                 // thread wait 
                wait ();
            } catch (InterruptedException e) {
            }
        }
        this.name = name + "----" + count + "+++++";
        System.out.println(Thread.currentThread().getName() + "..生产者..." + this.name);
        Flag = to true ;
         // wake of consumers waiting in 
        the this .notifyAll ();
    }

    public  the synchronized  void OUT () {
         // consume resources 
        the while (! Flag) {
             the try {
                 // thread to wait producers to produce resource 
                wait ();
            } catch (InterruptedException e) {
            }
        }
        System.out.println(Thread.currentThread().getName() + "...消费者..." + this.name);
        Flag = false ;
         // wake of consumers, production resources 
        the this .notifyAll ();
    }
}

//生产者
class Producer implements Runnable {
    private Resource rs;

    public Producer(Resource rs) {
        this.rs = rs;
    }

    // producers to produce resources 
    @Override
     public  void RUN () {
         the while ( to true ) {
            rs.set ( "trade" );
        }
    }
}

// consumer spending resource 
class Consumer the implements Runnable {
     Private Resource rs;

    public Consumer(Resource rs) {
        this.rs = rs;
    }

    // consumer spending resources 
    @Override
     public  void RUN () {
         the while ( to true ) {
            rs.out();
        }
    }
}

public class ProducerConsumerDemo {
    public static void main(String[] args) {
        Resource r = new Resource();
        Producer p = new Producer(r);
        Consumer c = new Consumer(r);
        Thread t1 = new Thread(p);
        Thread t2 = new Thread(c);
        t1.start();
        t2.start();
    }
}

Results are as follows:

Thread-0 .. commodity producers ... 1 +++++ ---- 
the Thread -1 ... consumer goods ... 1 +++++ ---- 
the Thread -0 producers .. ... 1 +++++ commodity ---- 
the Thread -1 ... consumer goods ... 1 +++++ ---- 
the Thread -0 .. commodity producers ... --- +++++ -1 
the Thread -1 ... consumer goods ... 1 +++++ ---- 
the Thread -0 .. 1 +++++ ---- commodity producers ...

 

Guess you like

Origin www.cnblogs.com/scar1et/p/11909355.html