【JUC并发编程01】JUC概述
文章目錄
- 1 進(jìn)程與線程的概念
- 1.1 進(jìn)程和線程
- 1.2 線程的狀態(tài)
- 1.3 wait和sleep
- 1.4 并發(fā)與并行
- 1.5 管程
- 1.6 用戶線程和守護(hù)線程
1 進(jìn)程與線程的概念
1.1 進(jìn)程和線程
進(jìn)程(Process)是計算機中的程序關(guān)于某數(shù)據(jù)集合上的一次運行活動,是系統(tǒng)進(jìn)行資源分配和調(diào)度的基本單位,是操作系統(tǒng)結(jié)構(gòu)的基礎(chǔ)。在早期面向進(jìn)程設(shè)計的計算機結(jié)構(gòu)中,進(jìn)程是程序的基本執(zhí)行實體;在當(dāng)代面向線程設(shè)計的計算機結(jié)構(gòu)中,進(jìn)程是線程的容器。程序是指令、數(shù)據(jù)及其組織形式的描述,進(jìn)程是程序的實體。
線程(英語:thread)是操作系統(tǒng)能夠進(jìn)行運算調(diào)度的最小單位。它被包含在進(jìn)程之中,是進(jìn)程中的實際運作單位。一條線程指的是進(jìn)程中一個單一順序的控制流,一個進(jìn)程中可以并發(fā)多個線程,每條線程并行執(zhí)行不同的任務(wù)。
總結(jié)來說:
進(jìn)程:指在系統(tǒng)中正在運行的一個應(yīng)用程序;程序一旦運行就是進(jìn)程;進(jìn)程是資源分配的最小單位
線程:系統(tǒng)分配處理器時間資源的基本單元,或者說進(jìn)程之內(nèi)獨立執(zhí)行的一個單元執(zhí)行流。線程時程序執(zhí)行的最小單位
1.2 線程的狀態(tài)
進(jìn)入 java.lang.Thread 類,找到內(nèi)部類 State ,代碼如下
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;}可以看出NEW(新建)、RUNNABLE(準(zhǔn)備就緒)、BLOCKED(阻塞)、WAITING(等待-不見不散)、
TIMED_WAITING(等待-過時不候)、TERMINATED(終結(jié))
1.3 wait和sleep
1.4 并發(fā)與并行
并發(fā):同一時間間隔內(nèi)多個線程正在執(zhí)行,實際上是宏觀上并行,微觀上串行
并行:同一時刻多個線程正在執(zhí)行
1.5 管程
保證了同一時刻只有一個進(jìn)程在管程內(nèi)活動,即管程內(nèi)定義的操作在同一時刻只被一個進(jìn)程調(diào)用(由編譯器實現(xiàn))。
1.6 用戶線程和守護(hù)線程
用戶線程:自定義線程
主線程結(jié)束了,用戶線程還在運行,jvm還存活
守護(hù)線程:比如說垃圾回收線程
沒有用戶線程了,只有守護(hù)線程,jvm結(jié)束
public class MyThread {public static void main(String[] args) {//使用Lambda 表達(dá)式實現(xiàn)這個接口,創(chuàng)建 線程t1Thread t1 = new Thread(() -> {//判斷是否是守護(hù)線程,(后臺運行的)System.out.println(Thread.currentThread().getName() + "::" + Thread.currentThread().isDaemon());while (true) {//主線程結(jié)束,程序還在運行,jvm 沒停止}}, "t1");// 把他設(shè)置為守護(hù)線程 ,主線程結(jié)束這個程序沒有用戶線程了,結(jié)束了t1.setDaemon(false);//啟動線程t1.start();System.out.println(Thread.currentThread().getName() +"結(jié)束");} }輸出結(jié)果為:
main結(jié)束
t1::false
總結(jié)
以上是生活随笔為你收集整理的【JUC并发编程01】JUC概述的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【IDEA】怎么把idea的目录结构,以
- 下一篇: 【JUC并发编程03】线程间通信