Java-12-多執行緒(執行緒建立)
阿新 • • 發佈:2021-03-30
Java-12-多執行緒(執行緒建立)
普通方法呼叫和多執行緒
普通方法呼叫只有主執行緒(main())一條執行道路,當其呼叫run()方法時,原來的事情被迫停止,等待run()執行完畢,
而多執行緒則呼叫start()方法,讓子執行緒執行run()方法,手上的事情不必停下來,多條線路並行執行
執行緒建立
- 繼承Thread類
- 實現Runnable介面
- 實現Callalbe介面
通過繼承Thread類建立執行緒
聲名一個類繼承Thread類
重寫run方法
建立執行緒物件,呼叫start方法啟動執行緒
//建立執行緒方式1 繼承Thread類,重寫run方法,呼叫start開啟執行緒
public class TestThread extends Thread{
//重寫run方法
@Override
public void run() {
//run()方法執行緒體
for (int i =0 ;i<20;i++){
System.out.println("我在讀書===="+i);
}
}
//main執行緒,主執行緒
public static void main(String[] args) {
//建立一個執行緒物件
TestThread testThread1 = new TestThread();
//呼叫start()開啟執行緒
testThread1.start();//常理來說,該方法呼叫在前,先執行,但多執行緒同時執行,所以輸出結果是交替的
for (int i=0;i<1000;i++){
System.out.println("我在聽音樂===="+i);
}
/*交替出現,由CPU排程,每次都不一樣
我在聽音樂====0
我在讀書====0
我在讀書====1
我在讀書====2
我在聽音樂====1
我在聽音樂====2
我在讀書====3
我在讀書====4
我在聽音樂====3
我在聽音樂====4
我在讀書====5
......
*/
}
}
案例:下載網圖
//多執行緒下載網圖
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
public class TestThread2 extends Thread{
private String url;
private String name;//儲存的檔名
//構造方法
public TestThread2(String url,String name){
this.url = url;
this.name = name;
}
//下載圖片執行緒的執行體
@Override
public void run() {
ImgDownloader imgDownloader = new ImgDownloader();
imgDownloader.downloader(url,name);
System.out.println("下載了檔名為:"+name);
}
//主執行緒
public static void main(String[] args) {
TestThread2 t1 = new TestThread2("https://store.hituyu.com/LqiGjDyCFyeQfqRyPYcnMaqJpIMtxsFK..png","1.jpg");
TestThread2 t2 = new TestThread2("https://store.hituyu.com/CqKCCwCkJhtBCnUoEUeBuGvUsqPWkZMC..png","2.jpg");
TestThread2 t3 = new TestThread2("https://store.hituyu.com/GgCPAtESyYKBXTgYxjBvuimKDUlqEcte..png","3.jpg");
t1.start();
t2.start();
t3.start();
/*
理想是1,2,3
但事實是:
下載了檔名為:3.jpg
下載了檔名為:2.jpg
下載了檔名為:1.jpg
*/
}
}
class ImgDownloader{
//下載方法
public void downloader(String url,String name){
//使用檔案工具類
try {
FileUtils.copyURLToFile(new URL(url),new File(name));
} catch (IOException e) {
e.printStackTrace();
System.out.println("IO異常,downloader wrong");
}
}
}
通過實現Runnable介面建立執行緒
定義一個類實現Runnable介面
實現run()方法,編寫執行緒執行體
建立執行緒物件,通過new Thread(執行緒物件).start()來呼叫start()方法啟動執行緒
推薦使用Runnable介面,Java單繼承具有侷限性
靈活方便,方便同一個物件被多個執行緒使用
//建立執行緒的方式2: 實現Runnable介面,重寫run方法,執行執行緒通過丟入Runnable介面的實現類,呼叫start
public class TestRunnable implements Runnable {
//重寫run方法
@Override
public void run() {
//run()方法執行緒體
for (int i =0 ;i<20;i++){
System.out.println("我在讀書===="+i);
}
}
//main執行緒,主執行緒
public static void main(String[] args) {
//建立Runnable介面的實現類物件
TestRunnable t = new TestRunnable();
//建立執行緒物件,通過執行緒物件來開啟執行緒,代理
new Thread(t).start();
for (int i=0;i<1000;i++){
System.out.println("我在聽音樂===="+i);
}
/*
輸出結果
...
我在讀書====0
我在聽音樂====96
我在讀書====1
我在聽音樂====97
...
*/
}
}
通過實現Callable介面建立執行緒
實現Callable介面,需要返回值型別
重寫call方法,需要丟擲異常
建立目標物件
建立執行服務:
ExecutorService ser = Executors.newFixedThreadPool(3);
提交執行:
Future<Boolean> r1 = ser.submit(t1);
獲取結果:
Boolean rs1 = r1.get();
關閉服務:
ser.shutdown();
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.*;
//執行緒建立方式3: 實現Callable介面
/*
* 可以定義返回值
* 可以丟擲異常
* */
public class TestCallable implements Callable<Boolean> {
private String url;
private String name;//儲存的檔名
//重寫call方法
@Override
public Boolean call() throws Exception {
ImgDownloader imgDownloader = new ImgDownloader();
imgDownloader.downloader(url,name);
System.out.println("下載了檔名為:"+name);
return true;
}
//構造方法
public TestCallable(String url,String name){
this.url = url;
this.name = name;
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
TestCallable t1 = new TestCallable("https://store.hituyu.com/LqiGjDyCFyeQfqRyPYcnMaqJpIMtxsFK..png", "1.jpg");
TestCallable t2 = new TestCallable("https://store.hituyu.com/CqKCCwCkJhtBCnUoEUeBuGvUsqPWkZMC..png", "2.jpg");
TestCallable t3 = new TestCallable("https://store.hituyu.com/GgCPAtESyYKBXTgYxjBvuimKDUlqEcte..png", "3.jpg");
//建立執行服務
ExecutorService ser = Executors.newFixedThreadPool(3);
//提交執行
Future<Boolean> r1 = ser.submit(t1);
Future<Boolean> r2 = ser.submit(t2);
Future<Boolean> r3 = ser.submit(t3);
//獲取結果
Boolean rs1 = r1.get();
Boolean rs2 = r2.get();
Boolean rs3 = r3.get();
//關閉服務
ser.shutdown();
}
}
class ImgDownloader{
//下載方法
public void downloader(String url,String name){
//使用檔案工具類
try {
FileUtils.copyURLToFile(new URL(url),new File(name));
} catch (IOException e) {
e.printStackTrace();
System.out.println("IO異常,downloader wrong");
}
}
}
模擬併發問題
//多執行緒操作同一個物件 併發
//買火車票
//發現問題,多個執行緒操作同一個資源的情況下,執行緒不安全,資料(重複紊亂)
public class TestConCurrent implements Runnable {
//票數
private int ticketNums = 10;
@Override
public void run() {
while (true){
if (ticketNums<=0){
break;
}
//模擬延時
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"拿到了倒數第"+ticketNums--+"張票");//獲取當前執行緒名
}
}
public static void main(String[] args) {
TestConCurrent t = new TestConCurrent();
new Thread(t,"小明").start();
new Thread(t,"小蘭").start();
new Thread(t,"小王").start();
}
}
模擬龜兔賽跑
通過執行緒休息得到我們想要的結果
//模擬龜兔賽跑
public class Race implements Runnable{
//勝者
private static String winner;
@Override
public void run() {
for(int i =0;i<=100;i++){
//模擬兔子休息
if (Thread.currentThread().getName().equals("兔")&& i%10==0){
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//判斷比賽是否結束
boolean flag = gameOver(i);
//比賽結束停止程式
if (flag){
break;
}
System.out.println(Thread.currentThread().getName()+"--->跑了"+i+"米");
}
}
//判斷是否完成比賽
private boolean gameOver(int meters){
//判斷是否有勝利者
if (winner!=null){
return true;
}else {
if(meters>=100) {
winner = Thread.currentThread().getName();
System.out.println(winner + "贏了");
return true;
}
return false;
}
}
public static void main(String[] args) {
Race race = new Race();
new Thread(race,"龜").start();
new Thread(race,"兔").start();
}
}