JUC笔记(1)

1、线程和进程

进程:一个程序,进程往往包含多个线程,至少包含一个

java 默认两个线程:main,GC

线程

Java能够开启线程吗?NO

 public synchronized void start() {
        /**
         * This method is not invoked for the main method thread or "system"
         * group threads created/set up by the VM. Any new functionality added
         * to this method in the future may have to also be added to the VM.
         *
         * A zero status value corresponds to state "NEW".
         */
        if (threadStatus != 0)
            throw new IllegalThreadStateException();

        /* Notify the group that this thread is about to be started
         * so that it can be added to the group's list of threads
         * and the group's unstarted count can be decremented. */
        group.add(this);

        boolean started = false;
        try {
            start0();
            started = true;
        } finally {
            try {
                if (!started) {
                    group.threadStartFailed(this);
                }
            } catch (Throwable ignore) {
                /* do nothing. If start0 threw a Throwable then
                  it will be passed up the call stack */
            }
        }
    }
//本地方法,底层调C++
    private native void start0();

2、并发和并行

并发:多个线程操作同一资源  

        CPU一核,模拟出来多条线程

 并行:多个人一起行走 

        CPU多核,多个线程可以同时执行,线程池

public class test1 {
    public static void main(String[] args) {
        //IO密集型
        System.out.println(Runtime.getRuntime().availableProcessors());
    }
}

并发编程的本质:充分利用CPU的资源

3、线程状态

public enum State {
        //新生
        NEW,

       //运行
        RUNNABLE,

        //阻塞
        BLOCKED,

        //等待
        WAITING,

        //时间等待
        TIMED_WAITING,

        //结束
        TERMINATED;
    }

4、wait和sleep区别

wait属于Object类,sleep在JUC中

sleep不释放锁,wait释放锁

wait不需要捕获异常,sleep需要捕获异常

wait需在同步代码块中,sleep随时睡

5、lock锁

synchronize锁

并发:多线程在同一个资源类,把资源丢入线程

lambda表达式(参数)->{代码}

synchronize和lock区别

synchronize是内置的关键字,lock是java类

synchronize无法判断锁的状态,lock能判断锁的状态

synchronize会自动获取锁,lock必须手动释放锁,如果不释放会发生死锁

synchronize 线程:获取锁,阻塞,没获取锁,等待,lock可以尝试获取锁tryLock

synchronize:可重入锁,不可以中断,非公平,lock,可重入锁,可以判断锁,非公平(可设置公平)

synchronize,适合锁少量同步代码 lock适合锁大量同步代码

6、消费者和生产者

package com.gykj;

public class ProandConsu {

    public static void main(String[] args) throws Exception{
        Data data = new Data();

        new Thread(()->{
            for(int i=0;i<5;i++){
                try {
                    data.increment();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        },"A").start();
        new Thread(()->{
            for(int i=0;i<5;i++){
                try {
                    data.decrement();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        },"B").start();

    }
}

//等待  业务  通知
class Data{
    private int number=0;

    public synchronized void increment() throws Exception{
        if(number != 0){
            this.wait();
        }
        number++;
        System.out.println(Thread.currentThread().getName()+"=>"+number);
        this.notifyAll();
    }

    public synchronized void decrement() throws Exception{
        if (number == 0){
            this.wait();
        }
        number--;
        System.out.println(Thread.currentThread().getName()+"=>"+number);
        this.notifyAll();
    }

}

但当我们有多个加一线程,多个减一线程时,会出现虚假唤醒的线程,原因是因为我们的number判断时,if只判断了一次,当等待的线程再次拿到资源时,不再判断number状态,直接进行下面操作,就出错了,解决方法就是将if改成while

package com.gykj;

public class ProandConsu {

    public static void main(String[] args) throws Exception{
        Data data = new Data();

        new Thread(()->{
            for(int i=0;i<5;i++){
                try {
                    data.increment();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        },"A").start();
        new Thread(()->{
            for(int i=0;i<5;i++){
                try {
                    data.decrement();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        },"B").start();

    }
}

//等待  业务  通知
class Data{
    private int number=0;

    public synchronized void increment() throws Exception{
        while(number != 0){
            this.wait();
        }
        number++;
        System.out.println(Thread.currentThread().getName()+"=>"+number);
        this.notifyAll();
    }

    public synchronized void decrement() throws Exception{
        while (number == 0){
            this.wait();
        }
        number--;
        System.out.println(Thread.currentThread().getName()+"=>"+number);
        this.notifyAll();
    }

}

虚假唤醒的解释我觉得这个就很好虚假唤醒及避免方式_u013978512的博客-CSDN博客_虚假唤醒虚假唤醒虚假唤醒就是当一个条件满足时,很多线程都被唤醒了,但是只有其中部分是有用的唤醒,其它的唤醒都是无用的。下面看一个生产者消费者的例子public static class Data { private int num = 0; public synchronized void increment() { if (num > 0) { try { Systehttps://blog.csdn.net/u013978512/article/details/113742623

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值