一个简单的多线程同步例子

/**
 * Created by pengyi on 2015/3/30 0030.
 */

public class Hello implements Runnable {
    private Integer amount = 50;
    private String s = "-";
    boolean a = true;
    public void run() {
        if ("线程一".equals(Thread.currentThread().getName()) ||
                "线程二".equals(Thread.currentThread().getName()) ||
                "线程三".equals(Thread.currentThread().getName()) ||
                "线程四".equals(Thread.currentThread().getName()) ||
                "线程五".equals(Thread.currentThread().getName()) ||
                "线程11".equals(Thread.currentThread().getName()) ||
                "线程12".equals(Thread.currentThread().getName()) ||
                "线程13".equals(Thread.currentThread().getName()) ||
                "线程14".equals(Thread.currentThread().getName()) ||
                "线程15".equals(Thread.currentThread().getName()) ||
                "线程21".equals(Thread.currentThread().getName()) ||
                "线程22".equals(Thread.currentThread().getName()) ||
                "线程23".equals(Thread.currentThread().getName()) ||
                "线程24".equals(Thread.currentThread().getName()) ||
                "线程25".equals(Thread.currentThread().getName()) ||
                "线程31".equals(Thread.currentThread().getName()) ||
                "线程32".equals(Thread.currentThread().getName()) ||
                "线程33".equals(Thread.currentThread().getName()) ||
                "线程34".equals(Thread.currentThread().getName()) ||
                "线程35".equals(Thread.currentThread().getName()) ||
                "线程41".equals(Thread.currentThread().getName()) ||
                "线程42".equals(Thread.currentThread().getName()) ||
                "线程43".equals(Thread.currentThread().getName()) ||
                "线程44".equals(Thread.currentThread().getName()) ||
                "线程45".equals(Thread.currentThread().getName())) {
            while (0 < amount) {
                if (a) {
                    a = false;
                    synchronized (this) {

                        amount = (amount - 1);
                        System.out.println(amount + "<---------->" + Thread.currentThread().getName());
                    }
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        System.out.println(e.getMessage());
                    }
                } else {
                    try {
                        synchronized (this) {
                            super.wait();
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                a = true;
                synchronized (this) {
                    super.notify();
                }
            }
        }else {
            while (100 > amount) {
                if (a) {
                    a = false;
                    synchronized (this) {

                        amount = (amount + 1);
                        System.out.println(amount + "<---------->" + Thread.currentThread().getName());
                    }
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        System.out.println(e.getMessage());
                    }
                } else {
                    try {
                        synchronized (this) {
                            super.wait();
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                a = true;
                synchronized (this) {
                    super.notify();
                }
            }
        }

    }

    public static void main(String[] args) {
        Hello he = new Hello();
        Thread thread1 = new Thread(he, "线程一");
        thread1.start();
        Thread thread2 = new Thread(he, "线程二");
        thread2.start();
        Thread thread3 = new Thread(he, "线程三");
        thread3.start();
        Thread thread4 = new Thread(he, "线程四");
        thread4.start();
        Thread thread5 = new Thread(he, "线程五");
        thread5.start();
        Thread thread6 = new Thread(he, "线程六");
        thread6.start();
        Thread thread7 = new Thread(he, "线程七");
        thread7.start();
        Thread thread8 = new Thread(he, "线程八");
        thread8.start();
        Thread thread9 = new Thread(he, "线程九");
        thread9.start();
        Thread thread10 = new Thread(he, "线程十");
        thread10.start();
        Thread thread11 = new Thread(he, "线程11");
        thread11.start();
        Thread thread12 = new Thread(he, "线程12");
        thread12.start();
        Thread thread13 = new Thread(he, "线程13");
        thread13.start();
        Thread thread14 = new Thread(he, "线程14");
        thread14.start();
        Thread thread15 = new Thread(he, "线程15");
        thread15.start();
        Thread thread16 = new Thread(he, "线程16");
        thread16.start();
        Thread thread17 = new Thread(he, "线程17");
        thread17.start();
        Thread thread18 = new Thread(he, "线程18");
        thread18.start();
        Thread thread19 = new Thread(he, "线程19");
        thread19.start();
        Thread thread20 = new Thread(he, "线程20");
        thread20.start();
        Thread thread21 = new Thread(he, "线程21");
        thread21.start();
        Thread thread22 = new Thread(he, "线程22");
        thread22.start();
        Thread thread23 = new Thread(he, "线程23");
        thread23.start();
        Thread thread24 = new Thread(he, "线程24");
        thread24.start();
        Thread thread25 = new Thread(he, "线程25");
        thread25.start();
        Thread thread26 = new Thread(he, "线程26");
        thread26.start();
        Thread thread27 = new Thread(he, "线程27");
        thread27.start();
        Thread thread28 = new Thread(he, "线程28");
        thread28.start();
        Thread thread29 = new Thread(he, "线程29");
        thread29.start();
        Thread thread30 = new Thread(he, "线程30");
        thread30.start();
        Thread thread31 = new Thread(he, "线程31");
        thread31.start();
        Thread thread32 = new Thread(he, "线程32");
        thread32.start();
        Thread thread33 = new Thread(he, "线程33");
        thread33.start();
        Thread thread34 = new Thread(he, "线程34");
        thread34.start();
        Thread thread35 = new Thread(he, "线程35");
        thread35.start();
        Thread thread36 = new Thread(he, "线程36");
        thread36.start();
        Thread thread37 = new Thread(he, "线程37");
        thread37.start();
        Thread thread38 = new Thread(he, "线程38");
        thread38.start();
        Thread thread39 = new Thread(he, "线程39");
        thread39.start();
        Thread thread40 = new Thread(he, "线程40");
        thread40.start();
        Thread thread41 = new Thread(he, "线程41");
        thread41.start();
        Thread thread42 = new Thread(he, "线程42");
        thread42.start();
        Thread thread43 = new Thread(he, "线程43");
        thread43.start();
        Thread thread44 = new Thread(he, "线程44");
        thread44.start();
        Thread thread45 = new Thread(he, "线程45");
        thread45.start();
        Thread thread46 = new Thread(he, "线程46");
        thread46.start();
        Thread thread47 = new Thread(he, "线程47");
        thread47.start();
        Thread thread48 = new Thread(he, "线程48");
        thread48.start();
        Thread thread49 = new Thread(he, "线程49");
        thread49.start();
        Thread thread50 = new Thread(he, "线程50");
        thread50.start();
    }
}

//开了50个线程,25个线程是+1,25个是-1,所以理论上是死循环 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值