Java并发编程——初识JUC

4 篇文章 0 订阅

初识JUC

一、什么是JUC

image-20200703233116773
JUC是java.util .concurrent包的简称,这是一个处理线程的工具包。

二、进程和线程

2.1 进程和线程

  • 进程:一个进程,qq.exe之类的程序的集合,一个进程往往可以包含很多个线程,至少包含一个
  • 线程:操作系统能够进行运算调度的最小单位

Java默认有两个线程,main和GC这两个线程

Java真的可以开启线程么?

Java本身没权权限开启线程,通过start()的源码可知,调用了本地方法,即调用了底层的C++

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 */
            }
        }
    }
	//native为本地方法,调用底层的C++,Java运行在虚拟机上,无法调用硬件
    private native void start0();

2.2 并行和并发

  • 并发(多个线程同时操作同一个资源)
    如果CPU只有一核,一次只能处理一条指令,通过快速切换虚拟出多条线程实现并发。
  • 并行(多个人一起行走): 并行提高效率,采用线程池
    CPU多核,多个线程可以同时执行
  • 如何获取系统CPU的核数:
public class Test1 {
    public static void main(String[] args) {
        //获取cpu核数
        //CPU密集型,IO密集型
        System.out.println(Runtime.getRuntime().availableProcessors());
    }
}

并发编程的本质:充分利用CPU的资源
为什么要并发编程:CPU/内存/IO设备的速度有极大差异,为了合理利用CPU的高性能,操作系统增加了进程/线程.

2.3 Java线程有几个状态

通过源码可以知道线程有六个状态

Thread.State.NEW;
public enum State {
        /**
         * Thread state for a thread which has not yet started.
         */
        NEW,		//线程新生
        RUNNABLE,	//线程运行
        BLOCKED,	//线程阻塞
        WAITING,	//等待,死死的等待
        TIMED_WAITING,	//超时等待
        TERMINATED;		//终止
}

2.4 wait/sleep的区别

  • 来自不同的类
    wait ==> Object
    sleep ==> Thread
  • 关于锁的释放
    wait会释放锁,sleep睡着了,抱着锁睡觉,不会释放锁
  • 使用的范围不同
    wait:必须在同步代码块中使用,必须又可以等待的对象
    sleep:任何时候都可以睡
  • 是否需要捕获异常
    wait 不需要捕获异常;
    sleep需要捕获异常InterruptedException(属于checked exception:指的是编译时异常),进程在休眠的过程中不可被打断,被打断则抛出异常。
public static void main(String[] args){
        Thread a = new Thread(){
            @Override
            public void run(){
                try {
                    System.out.println("正在执行线程");
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    System.out.println("休眠的进程被打断");
                }
            }
        };
        a.start();
        a.interrupt();
    }

正在执行线程
休眠的进程被打断
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at gdut.hzh.demo01.Test1$1.run(Test1.java:17)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值