关于线程中加锁的详解及示例

在这里插入图片描述

import java.util.Scanner;
public class WaitDemo {
    private static Object object = new Object();//调用object.wait();方法时要先创建对象

    public static class A extends Thread {
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                System.out.println(i);
            }
            // 打印完后要等待 B 线程启动,并完成某个条件
            synchronized (object) {//要等哪个对象,就要先对其加锁
                try {
                    //有等待就要有唤醒
                    object.wait();//用object调用这个方法等待,等之前要给对象先加锁,保证线程安全
                } catch (InterruptedException e) {
                    //抛出的异常(与线程中断有关),如果一直等的时候,用户要求它停下来,它就会收到一个异常InterruptedException
                    e.printStackTrace();
                }
            }
            for (int i = 100; i < 110; i++) {
                System.out.println(i);
            }
        }
    }

    public static void main(String[] args) {
        Thread a = new A();
        a.start();
        //程序运行到这里是一直等待的状态
        Scanner scanner = new Scanner(System.in);
        System.out.println("我不输入,A 线程就绝对不会动");
        scanner.nextLine();//这个方法是要求输入一个什么
        //输入一个字符,程序才会继续
        synchronized (object) {
            object.notify();//调用这个方法唤醒程序(唤醒正在等待对象监视器的单个线程。 ),object指向某一个对象,会找到这个对象中的线程(其中的一个,不是所有),把线程的状态切换到RUNNABLE.但并不是马上执行,需要main线程把锁释放后才可执行。
            //object.notifyAll();唤醒正在等待对象监视器的所有线程。
        }
    }
}
//只有输出一个字符之后才会接着打印后面的

在这里插入图片描述

public class NWaitDemo {
    private static int n=0;
    private static Object o=new Object();
    private static class Sub extends Thread{
        Sub(){//给线程起个名字
            super(("n--"));
        }
        @Override
        public void run() {
            while(true){
                synchronized (o) {
                    if(n==0){//因为范围在[0,10],所以当n=0时应WAITING
                        try {
                            o.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    n--;
                    System.out.println(getName() + ":" + n);
                    if(n==9){//到n=9的时候再唤醒
                        o.notify();
                    }
                }
            }
        }
    }
    private static class Add extends Thread{
        Add(){
            super("n++");
        }
        @Override
        public void run() {
            while(true){
                synchronized (o){
                    if(n==10){//因为范围在[0,10],所以当n=10时应WAITING
                        try {
                            o.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    n++;
                    System.out.println(getName()+":"+n);
                    if(n==1){//到n=1的时候再唤醒
                        o.notify();
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        Thread a = new Add();
        Thread b = new Sub();
        a.start();
        b.start();
    }
}
//打印出的结果没有大于10,小于0的
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值