线程打印

就是突然想到以前看到过一道面试题似乎是用多线层打印出alibaba这个单词,然后网上搜不到,搜到了这个2个线程打印出1-100 ,就尝试了下记录一下

最初用syn和wait和notify方法组合的时候遇到了特别多的坑

因为wait 和notify会释放锁 然后我就直接想当然得

 @Override
    public void run() {
        while (i <= 100) {
            synchronized (lock) {
                try {
                    System.out.println(Thread.currentThread().getName() + "--------" + i++);
                    notifyAll();
                    wait();
                } catch (Exception e) {
                    e.printStackTrace();
                    System.exit(0);
                }
            }
        }
    }

结果就报错IllegalMonitorStateException

这原因就是因为notify的时候并没有得到锁对象 前面我就特别迷 因为在syn里面就应该是有了锁对象了,然后我用this代替obj尝试 就陷入了死锁

 @Override
    public void run() {
        while (i <= 100) {
            synchronized (this) {
                try {
                    System.out.println(Thread.currentThread().getName() + "--------" + i++);
                    notifyAll();
                    wait();
                } catch (Exception e) {
                    e.printStackTrace();
                    System.exit(0);
                }
            }
        }
    }

!!!!!!!!!!!最后就突然觉悟的发现了 syn锁的是什么对象就应该用那个对象去调用wait和notify

我这里的this出现死锁是因为notify通知的是因为这个锁出现阻塞的线程(我以前一直以为通知的是所有的线程)

package zz;


/**
 * @DATE: 2020/5/6
 **/
public class ThreadDemo {


    public static void main(String[] args)throws Exception {
        new Thread(new MyRun(), "线程1").start();
        new Thread(new MyRun(), "线程>>>2").start();
    }
}


class MyRun implements Runnable {

    static Object lock = new Object();
    static volatile int i = 0;
    @Override
    public void run() {

        while (i <= 100) {
            synchronized (lock) {
                try {
                    System.out.println(Thread.currentThread().getName() + "--------" + i++);
                    lock.notifyAll();
                    lock. wait();
                } catch (Exception e) {
                    e.printStackTrace();
                    System.exit(0);
                }

            }
        }
    }
}

前面想到了syn是在多线程的情况下就是重量级锁,于是想到了能不能优化,分2种情况吧

1是不用锁 2是使用轻锁 或者是cas进行实现

package zz;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @DATE: 2020/5/6
 **/
public class ThreadDemo {
    static volatile int i = 0;
    static volatile boolean flag = true;

    public static void main(String[] args)throws Exception {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (i<=100){
                    if (flag) {
                        System.out.println(Thread.currentThread().getName()+"----------"+i++);
                        flag=!flag;
                    }
                }
            }
        }, "线程1").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (i<=100){
                    if (!flag) {
                        System.out.println(Thread.currentThread().getName()+"+++"+i++);
                        flag=!flag;
                    }
                }
            }
        }, "线程>>2").start();
    }
}


class MyRun implements Runnable {

    static Lock lock = new ReentrantLock();
    static volatile int i = 0;
    static volatile boolean flag = true;

    @Override
    public void run() {

        while (i <= 100) {
            if (flag)
                System.out.println(i++);
            flag=!flag;
        }
    }

}
package zz;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @DATE: 2020/5/6
 * @author: zhanghong
 **/
public class ThreadDemo {
    static volatile int i = 0;
    static volatile boolean flag = true;

    public static void main(String[] args)throws Exception {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (i<=100){
                    if (flag) {
                        System.out.println(Thread.currentThread().getName()+"=========="+i++);
                        flag=!flag;
                    }
                }
            }
        }, "线程1").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (i<=100){
                    if (!flag) {
                        System.out.println(Thread.currentThread().getName()+"=========="+i++);
                        flag=!flag;
                    }
                }
            }
        }, "线程2").start();
    }
}


class MyRun implements Runnable {

    static Lock lock = new ReentrantLock();
    static volatile int i = 0;
    static volatile boolean flag = true;

    @Override
    public void run() {

        while (i <= 100) {
            if (flag)
                System.out.println(i++);
            flag=!flag;
        }
    }

}
package zz;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @DATE: 2020/5/6
 * @author: zhanghong
 **/
public class ThreadDemo {
    static volatile int i = 0;
    static volatile boolean flag = true;

    public static void main(String[] args)throws Exception {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (i<=100){
                    if (flag) {
                        System.out.println(Thread.currentThread().getName()+"----------"+i++);
                        flag=!flag;
                    }
                }
            }
        }, "线程1").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (i<=100){
                    if (!flag) {
                        System.out.println(Thread.currentThread().getName()+"+++"+i++);
                        flag=!flag;
                    }
                }
            }
        }, "线程>>2").start();
    }
}


class MyRun implements Runnable {

    static Lock lock = new ReentrantLock();
    static volatile int i = 0;
    static volatile boolean flag = true;

    @Override
    public void run() {

        while (i <= 100) {
            if (flag)
                System.out.println(i++);
            flag=!flag;
        }
    }

}

juc 还不会。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值