1、進程
進程是執(zhí)行程序的一次執(zhí)行過程,是系統(tǒng)資源分配的單位。
2、線程
一個進程可以包含多個線程,一個進程至少有一個線程。線程是CPU調(diào)度和執(zhí)行的單位。
二、線程的創(chuàng)建 1、Thread:繼承Thread類,Thread類實現(xiàn)了Runable接口。方式:自定義線程類繼承Thread 》重寫run方法,編寫線程執(zhí)行體 》 創(chuàng)建線程對象調(diào)用start
public class MyThread extends Thread{
@Override
public void run(){
for (int i = 0; i< 20; i++) {
System.out.println("這是一個線程"+i);
}
}
public static void main(String[] args) {
//main主線程
for (int i = 0; i< 20; i++) {
System.out.println("這是一個線程"+i);
}
//副線程
MyThread myThread=new MyThread();
// myThread.run();
//開啟線程
myThread.start();
}
}
方式:自定義類實現(xiàn)Runnable 》重寫run方法,編寫線程執(zhí)行體 》 創(chuàng)建線程對象調(diào)用start
public class RunnableTest implements Runnable{
@Override
public void run() {
for (int i = 0; i< 20; i++) {
System.out.println("測試"+i);
}
}
public static void main(String[] args) {
RunnableTest runnableTest=new RunnableTest();
Thread thread=new Thread(runnableTest);
thread.start();
for (int i = 0; i< 200; i++) {
System.out.println("主線程在運行"+i);
}
}
}
3、Callable:實現(xiàn)Callable接口。 ?????實現(xiàn)Callable接口,需要返回值類型。
重寫call方法需要拋出異常。
public class CallabaleTest implements Callable{
@Override
public Boolean call() throws Exception {
System.out.println("副線程執(zhí)行");
return true;
}
public static void main(String[] args) {
CallabaleTest t1=new CallabaleTest();
CallabaleTest t2=new CallabaleTest();
CallabaleTest t3=new CallabaleTest();
//創(chuàng)建執(zhí)行服務
ExecutorService service= Executors.newFixedThreadPool(3);
//提交執(zhí)行
Futurer1=service.submit(t1);
Futurer2=service.submit(t2);
Futurer3=service.submit(t3);
//獲取結(jié)果
try {
Boolean aBoolean = r1.get();
Boolean bBoolean = r2.get();
Boolean cBoolean = r3.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
//關閉
service.shutdown();
}
}
三、線程狀態(tài)方式:建議線程正常停止、建議使用標志位、不要使用stop和destroy。
例子:使用標志位
public class ThreadStop implements Runnable{
//標志位
private Boolean flag=true;
@Override
public void run() {
while (flag){
System.out.println("running");
}
}
//停止方法
public void stop(){
flag=false;
}
public static void main(String[] args) {
//開啟線程
ThreadStop stop=new ThreadStop();
new Thread(stop).start();
for (int i = 0; i< 10000; i++) {
if(i==900){
stop.stop();
break;
}
}
}
}
2、線程休眠sleep指定當前線程阻塞的毫秒數(shù);
sleep存在異常InterruptedException;
sleep時間達到后線程進入就緒狀態(tài);
sleep可以模擬網(wǎng)絡延時,計時器等;
每個對象都有一個鎖,sleep不會釋放鎖;
3、線程禮讓禮讓線程,讓當前正在執(zhí)行的線程暫停,但不阻塞;
將線程從運行狀態(tài)轉(zhuǎn)為就緒狀態(tài);
讓CPU重新調(diào)度,禮讓不一定成功。
public class ThreadStop implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"線程開始執(zhí)行");
Thread.yield();//線程禮讓
System.out.println(Thread.currentThread().getName()+"線程結(jié)束執(zhí)行");
}
public static void main(String[] args) {
//開啟線程
ThreadStop stop=new ThreadStop();
new Thread(stop,"1").start();
new Thread(stop,"2").start();
}
}
4、線程強制執(zhí)行(Join)join合并線程,待此線程執(zhí)行完成后,再執(zhí)行其他線程,其他線程阻塞。類似插隊。
public class ThreadStop implements Runnable{
@Override
public void run() {
for (int i = 0; i< 10; i++) {
System.out.println("副線程");
}
}
public static void main(String[] args) {
//開啟線程
ThreadStop stop=new ThreadStop();
Thread thread = new Thread(stop, "1");
thread.start();
for (int i = 0; i< 100; i++) {
System.out.println("主線程");
if(i==20){
try {
//此時會讓副線程執(zhí)行完再執(zhí)行主線程
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
5、線程狀態(tài)觀測Thread.State state=thread.getState();
線程狀態(tài):
NEW:尚未啟動的線程處于此狀態(tài)
RUNNABLE:在JAVA虛擬機中執(zhí)行的線程處于此狀態(tài)
BLOCKED:被阻塞等待監(jiān)視器鎖定的線程處于此狀態(tài)
WAITING:正在等待另一個線程執(zhí)行特定動作的線程處于此狀態(tài)
TIMED WAITING:正在等待另一個線程執(zhí)行動作達到指定等待時間的線程處于此狀態(tài)
TERMINATED:已退出線程
6、線程的優(yōu)先級Java提供一個線程調(diào)度器來監(jiān)控程序中啟動后進入就緒狀態(tài)的所有線程,線程調(diào)度器按照優(yōu)先級決定應該調(diào)度那個線程來執(zhí)行。
線程的優(yōu)先級用數(shù)字表示,范圍1-10
Thread.MIN_PROIRITY=1;
Thread.MAX_PROIRITY=10;
Thread.NORM_PROIRITY=5;
使用以下方式改變和獲取優(yōu)先級
getPriority() ; setPriority(int xxx);
注意:優(yōu)先級低只是意味獲得調(diào)度的概率低,并不是優(yōu)先級低就不會被調(diào)用,都看CPU掉度。
public class ThreadStop implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
}
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
//開啟線程
ThreadStop stop=new ThreadStop();
Thread thread1 = new Thread(stop, "1");
Thread thread2 = new Thread(stop, "2");
Thread thread3 = new Thread(stop, "3");
Thread thread4 = new Thread(stop, "4");
Thread thread5 = new Thread(stop, "5");
thread1.setPriority(1);
thread2.setPriority(10);
thread3.setPriority(Thread.MIN_PRIORITY);
thread4.setPriority(Thread.MAX_PRIORITY);
thread1.start();
thread2.start();
thread3.start();
thread4.start();
thread5.start();
}
}
7、守護(daemon)線程線程分為用戶線程和守護線程
虛擬機必須確保用戶線程執(zhí)行完畢
虛擬機不用等待守護線程執(zhí)行完畢
作用后臺操作日志、監(jiān)控內(nèi)存、垃圾回收 ???
Thread thread=new Thread();
//默認false表示用戶線程
thread.setDaemon(true);
你是否還在尋找穩(wěn)定的海外服務器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務器高可用性,企業(yè)級服務器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧
文章名稱:Java多線程基本知識--概念、創(chuàng)建-創(chuàng)新互聯(lián)
本文網(wǎng)址:http://jinyejixie.com/article10/gpego.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、品牌網(wǎng)站建設、手機網(wǎng)站建設、動態(tài)網(wǎng)站、域名注冊、網(wǎng)站設計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)