JUC并发编程一:什么是JUC+进程/线程(Java)
1、什么是JUC
- JUC的意思就是java并发编程工具包,与JUC相关的有三个包:java.util.concurrent、java.util.concurrent.atomic、java.util.concurrent.locks。实现多线程有三种方式:Thread、Runnable、Callable,其中Callable就位于concurrent包下
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Asbul3a9-1627111353075)(C:%5CUsers%5C%E9%99%88%E7%B4%A0%E5%A8%9C%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5Cimage-20210509141607638.png)]
2、进程/线程
-
进程:进程是一个具有一定独立功能的程序关于某个数据集合的一次运行活动。它是操作系统动态执行的基本单元,在传统的操作系统中,进程既是基本的分配单元,也是基本的执行单元。
-
线程:通常在一个进程中可以包含若干个线程,当然一个进程中至少有一个线程,不然没有存在的意义。线程可以利用进程所拥有的资源,在引入线程的操作系统中,通常都是把进程作为分配资源的基本单位,而把线程作为独立运行和独立调度的基本单位,由于线程比进程更小,基本上不拥有系统资源,故对它的调度所付出的开销就会小得多,能更高效的提高系统多个程序间并发执行的程度。
-
进程的例子:一个程序,QQ.exe,Music.exe等程序的集合
-
一个进程往往可以包含多个线程,至少包含一个!
-
Java默认有两个线程(main,GC)
-
GC的基本原理: 1. 对于程序员来说,用new关键字即在堆中分配了内存,我们称之为“可达”。对于GC来说,只要所有被引用的对象为null时,我们称之为“不可达”,就将进行内存的回收。 2. 当一个对象被创建时,GC开始监控这个对象的大小、内存地址及使用情况。GC采用有向图的方式记录和管理堆(heap)中的所有对象,通过这种方式可以明确哪些对象是可达的,哪些不是。当确定为不可达时,则对其进行回收。 3. 保证GC在不同平台的实现问题,java规范对其很多行为没有进行严格的规定。对于采用什么算法,什么时候进行回收等。
-
-
线程的例子:如在使用word的时候突然电脑关机,重新开机的时候word会重新恢复当前的数据
-
对于Java而已实现多线程有三种方式:Thread、Runnable、Callable。
-
但java是开不了线程的!!!
-
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++,JAVA是无法直接操作硬件 private native void start0();
并发、并行
并发编程:并发、并行
1、并发(多线程操作同一个资源)
- CPU一核,模拟出来多条线程,快速交替
2、并行(多个人一起行走)
- CPU多核,多个线程可以同时执行;线程池
-
package Demo01;
public class Test01 {
public static void main(String[] args) {
//获取CPU的核数
//CPU密集型,IO密集型
System.out.println(Runtime.getRuntime().availableProcessors());//4
// TimeUnit.DAYS.sleep(1); //这个程序执行一天
// TimeUnit.SECONDS.sleep(1); //这个程序执行一秒
}
}
3、并发编程的本质:充分利用CPU的资源
➢线程的状态:
public enum State {
/**
* Thread state for a thread which has not yet started.
*/
//新生
NEW,
/**
* Thread state for a runnable thread. A thread in the runnable
* state is executing in the Java virtual machine but it may
* be waiting for other resources from the operating system
* such as processor.
*/
//运行
RUNNABLE,
/**
* Thread state for a thread blocked waiting for a monitor lock.
* A thread in the blocked state is waiting for a monitor lock
* to enter a synchronized block/method or
* reenter a synchronized block/method after calling
* {@link Object#wait() Object.wait}.
*/
//堵塞
BLOCKED,
/**
* Thread state for a waiting thread.
* A thread is in the waiting state due to calling one of the
* following methods:
* <ul>
* <li>{@link Object#wait() Object.wait} with no timeout</li>
* <li>{@link #join() Thread.join} with no timeout</li>
* <li>{@link LockSupport#park() LockSupport.park}</li>
* </ul>
*
* <p>A thread in the waiting state is waiting for another thread to
* perform a particular action.
*
* For example, a thread that has called <tt>Object.wait()</tt>
* on an object is waiting for another thread to call
* <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
* that object. A thread that has called <tt>Thread.join()</tt>
* is waiting for a specified thread to terminate.
*/
//等待(一直处于等待状态)
WAITING,
/**
* Thread state for a waiting thread with a specified waiting time.
* A thread is in the timed waiting state due to calling one of
* the following methods with a specified positive waiting time:
* <ul>
* <li>{@link #sleep Thread.sleep}</li>
* <li>{@link Object#wait(long) Object.wait} with timeout</li>
* <li>{@link #join(long) Thread.join} with timeout</li>
* <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
* <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
* </ul>
*/
//超时等待
TIMED_WAITING,
/**
* Thread state for a terminated thread.
* The thread has completed execution.
*/
//终止
TERMINATED;
}
➢wait/sleep的区别
1、来自不同的类
- wait=>Object
- sleep=>Thread
2、关于锁的释放
- wait会释放锁
- sleep不会释放锁
3、使用的范围是不同的
- wait必须在同步代码块中
- sleep可以在任意地方
4、是否需要捕获异常
- wait不需要捕获异常
- sleep必须要捕获异常