Java基础巩固系列 线程的通信

代码示例:

//线程通信。如下的三个关键字使用的话,都得在同步代码块或同步方法中。
//wait():一旦一个线程执行到wait(),就释放当前的锁。
//notify()  notifyAll():唤醒wait的一个或所有的线程
//使用两个线程打印1-100。线程1,线程2 交替打印
class PrintNumber implements Runnable {
    int num = 1;

    @Override
    public void run() {
        while (true) {
            synchronized (this) {
                notify();
                if (num <= 100) {
                    try {
                        Thread.currentThread().sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + ":" + num);
                    num++;
                } else {
                    break;
                }
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

public class TestCommunication {
    public static void main(String[] args) {
        PrintNumber p = new PrintNumber();
        Thread t1 = new Thread(p);
        Thread t2 = new Thread(p);

        t1.setName("甲");
        t2.setName("乙");

        t1.start();
        t2.start();
    }
}

结果:

甲:1
乙:2
甲:3
乙:4
甲:5
乙:6
甲:7
乙:8
甲:9
乙:10
甲:11
乙:12
甲:13
乙:14
甲:15
乙:16
甲:17
乙:18
甲:19
乙:20
甲:21
乙:22
甲:23
乙:24
甲:25
乙:26
甲:27
乙:28
甲:29
乙:30
甲:31
乙:32
甲:33
乙:34
甲:35
乙:36
甲:37
乙:38
甲:39
乙:40
甲:41
乙:42
甲:43
乙:44
甲:45
乙:46
甲:47
乙:48
甲:49
乙:50
甲:51
乙:52
甲:53
乙:54
甲:55
乙:56
甲:57
乙:58
甲:59
乙:60
甲:61
乙:62
甲:63
乙:64
甲:65
乙:66
甲:67
乙:68
甲:69
乙:70
甲:71
乙:72
甲:73
乙:74
甲:75
乙:76
甲:77
乙:78
甲:79
乙:80
甲:81
乙:82
甲:83
乙:84
甲:85
乙:86
甲:87
乙:88
甲:89
乙:90
甲:91
乙:92
甲:93
乙:94
甲:95
乙:96
甲:97
乙:98
甲:99
乙:100

 

 

代码示例2:

 

public class TestAccount {
    public static void main(String[] args) {
        Account account = new Account();
        Customer c1 = new Customer(account);
        Customer c2 = new Customer(account);

        c1.setName("甲");
        c2.setName("乙");

        c1.start();
        c2.start();
    }
}

class Customer extends Thread {
    Account account;

    public Customer(Account account) {
        this.account = account;
    }

    @Override
    public void run() {
        for (int i = 0; i < 3; i++) {
            account.deposit(1000);
        }
    }
}

class Account {
    double balance;//余额

    //存钱 //扩展:实现二者交替打印,使用线程的通信
    public synchronized void deposit(double amt) {  //是唯一的Account对象就可以加synchronized
        notify();
        balance += amt;
        try {
            Thread.currentThread().sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + ":" + balance);
        try {
            wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

结果:

甲:1000.0
乙:2000.0
甲:3000.0
乙:4000.0
甲:5000.0
乙:6000.0

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值