Java那些事儿

java线程间的通信

2017-09-21  本文已影响0人  文艺小年青
package xianPack;

public class Test0 {

    //线程间的通信
    public static void main(String[] args) {
        
        Runnable5 runnable5 = new Runnable5();
        runnable5.age = 10;
        
        Test0 test0 = new Test0();
        runnable5.test0 = test0;
        
        Thread thread = new Thread(runnable5);
        thread.start();
    }
    void printAge(int age) {
        System.out.println(age);
    }
    
}

class Runnable5 implements Runnable {
    int age;
    Test0 test0;
    @Override
    public void run() {
        System.out.println(this.age);
        this.age++;
        this.test0.printAge(this.age);
    }
}

上一篇下一篇

猜你喜欢

热点阅读