Multithreading share data

 Multiple threads behavior consistent with a common operating data, the most classic example of this is the ticket

public class ShareData {
    private int num  =10;
    public synchronized void inc(){
        num++;
        System.out.println(Thread.currentThread().getName()+"调用inc这个方法num = "+num);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
package com.example.threadTest;

/**
 * Description: hibernate
 * Created by lenovo on 2019/6/1 19:33
 */
public class Ticket implements Runnable{
    private ShareData shareData;

    public Ticket(ShareData shareData) {
        this.shareData = shareData;
    }

    public Ticket() {

    }

    @Override
    public void run() {
        for (int i=0;i<5;i++){
            shareData.inc();
        }
    }
    public static void main (String[] args){
        ShareData shareData = new ShareData();
        for (int i=0;i<4;i++){
          new Thread(new Ticket(shareData),"Thread"+i).start();
      }
    }


}

2. Multiple threads inconsistent behavior, a common operating data

If the code is different for each thread of execution this time on the need to call a different Runnable objects, there are two ways to share data between the Runnable object

, The shared data is encapsulated in another object, then transferred one by one to each of these objects Runnable object, a method of operating each thread to be assigned to a shared data body to accomplish that object, so that it is easy to implement for

And mutually exclusive of each operation of the data communication performed.

Hey, look at this summary will look to achieve dawned.

Add different implementations in which the data object sharing

 

package com.example.demo1;

/**
 * Description: hibernate
 * Created by lenovo on 2019/6/1 19:57
 */
public class MyThread implements Runnable{
    private SharaData sharaData;
    public MyThread() {

    }

    public MyThread(SharaData sharaData) {
        this.sharaData = sharaData;
    }

    public static void main (String[] args){

        SharaData sharaData = new SharaData();
        for (int i=0;i<4;i++){
            if (i%2==0){
                new Thread(new MyThread(sharaData),"Thread"+i).start();
            }else{
                new Thread(new MyThreadDec(sharaData),"Thread"+i).start();
            }
        }
    }
    //封装共享数据类



    @Override
    public void run() {
    for (int i=0;i<5;i++){
        sharaData.inc();

    }
    }
}

 

//
package com.example.demo1;
/** * Description: hibernate * Created by lenovo on 2019/6/1 20:07 */ public class MyThreadDec implements Runnable{ //封装共享数据 private SharaData sharaData; public MyThreadDec(SharaData sharaData) { this.sharaData = sharaData; } @Override public void run() { for (int i=0;i<5;i++){ sharaData.dec(); } } }
// shared data class 
Package com.example.demo1;
/ * * * the Description: Hibernate * the Created by Lenovo ON 2019/6/1 19:57 * / public class SharaData { Private int NUM = 10 ; public the synchronized void inc is () { NUM ++ ; . the System OUT .println (Thread.currentThread () getName () +. " call this method inc = NUM " + NUM); the try { the Thread.sleep ( 1000 ); } the catch (InterruptedException e) { e.printStackTrace(); } } public synchronized void dec(){ num++; System.out.println(Thread.currentThread().getName()+"调用inc这个方法num = "+num); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }

 

The second method

These Runnable object as a class, inner class, sharing data as member variables of the outer class, method of operation you each thread to shared data is also assigned to the external class ,,

In order to achieve mutual exclusion to shared data and various communications operations, call these methods as respective external Runnable object classes within the class

package com.example.demo1;

/**
 * Description: hibernate
 * Created by lenovo on 2019/6/1 20:33
 */
public class Thread2 {
    public static void main (String[] args){
        final SharaData sharaData =  new SharaData();
       for (int i=0;i<4;i++){
           if (i%2==0){
               new Thread(new Runnable() {
                   @Override
                   public void run() {
                       for (int i=0;i<5;i++){
                           sharaData.inc();
                       }
                   }
               },"Thread"+i).start();
           }else{
                 new Thread(new Runnable() {
                     @Override
                     public void run() {
                         for (int i=0;i<5;i++){
                             sharaData.dec();
                         }
                     }
                 },"Thread"+i).start();
           }
       }
    }

}

 

Guess you like

Origin www.cnblogs.com/zhulina-917/p/10960888.html