1. 程式人生 > 其它 >多執行緒之執行緒建立

多執行緒之執行緒建立

執行緒建立

  • 繼承Thread類

    package com.yeyue.demo01;

    //建立執行緒方式一:繼承Thread類,重寫run()方法,呼叫start開啟執行緒

    //執行緒開始不一定立即執行,由cpu排程

    public class TestThread1 extends Thread {
    @Override
    public void run() {
    //run方法執行緒體
    for (int i = 0; i < 20; i++) {
    System.out.println("我在看程式碼---"+i);
    }
    }

    public static void main(String[] args) {
    //main 執行緒,主執行緒

    //建立一個執行緒物件
    TestThread1 testThread1 = new TestThread1();

    //呼叫start()方法開啟執行緒
    testThread1.start();

    for (int i = 0; i < 100; i++) {
    System.out.println("我在學習多執行緒----" + i);
    }
    }
    }

  • 實現Runnable介面

    多執行緒下載圖片
    package com.yeyue.demo01;

    import org.apache.commons.io.FileUtils;

    import java.io.File;
    import java.io.IOException;
    import java.net.URL;

    //練習Thread,實現多執行緒同步下載圖片
    public class TestThread2 implements Runnable {
    private String url; //圖片下載的url
    private String name; //下載的圖片儲存的圖片名字


    public TestThread2(String url,String name){
    this.url = url;
    this.name = name;
    }

    //下載圖片的執行體
    @Override
    public void run() {
    WebDownloader webDownloader = new WebDownloader();
    webDownloader.downloader(url,name);
    System.out.println("下載了檔名:" + name);
    }

    public static void main(String[] args) {
    TestThread2 t1 = new TestThread2("https://img2.baidu.com/it/u=2253502056,189588800&fm=26&fmt=auto&gp=0.jpg","1.jpg");
    TestThread2 t2 = new TestThread2("https://img2.baidu.com/it/u=3529414605,308711291&fm=26&fmt=auto&gp=0.jpg","2.jpg");
    TestThread2 t3 = new TestThread2("https://img1.baidu.com/it/u=1863405005,3705892836&fm=26&fmt=auto&gp=0.jpg","3.jpg");

    new Thread(t1).start();
    new Thread(t2).start();
    new Thread(t3).start();

    }

    }

    //下載器
    class WebDownloader{
    //下載方法
    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方法出現問題");
    }
    }
    }
    買票
    package com.yeyue.demo01;

    //多個執行緒同時操作一個物件
    //買火車票的例子

    //多個執行緒操作同一個資源的情況下,資源不安全了
    public class TestThread4 implements Runnable {

    //票數
    private int ticketNums =10;

    @Override
    public void run() {
    while (true){
    if(ticketNums <= 0){
    break;
    }
    try {
    Thread.sleep(200);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    System.out.println(Thread.currentThread().getName()+"-->拿到了第"+ticketNums--+"票");
    }
    }

    public static void main(String[] args) {
    TestThread4 ticket = new TestThread4();

    new Thread(ticket,"小明").start();
    new Thread(ticket,"老師").start();
    new Thread(ticket,"黃牛黨").start();
    }
    }
  • 實現Callable介面

    package com.yeyue.demo01;

    import org.apache.commons.io.FileUtils;

    import java.io.File;
    import java.io.IOException;
    import java.net.URL;
    import java.util.concurrent.*;

    public class TestCallable implements Callable<Boolean> {
    private String url; //圖片下載的url
    private String name; //下載的圖片儲存的圖片名字


    public TestCallable(String url,String name){
    this.url = url;
    this.name = name;
    }

    //下載圖片的執行體
    @Override
    public Boolean call() {
    WebDownloader1 webDownloader1 = new WebDownloader1();
    webDownloader1.downloader1(url,name);
    System.out.println("下載了檔名:" + name);
    return true;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
    TestCallable t1 = new TestCallable("https://img2.baidu.com/it/u=2253502056,189588800&fm=26&fmt=auto&gp=0.jpg","1.jpg");
    TestCallable t2 = new TestCallable("https://img2.baidu.com/it/u=3529414605,308711291&fm=26&fmt=auto&gp=0.jpg","2.jpg");
    TestCallable t3 = new TestCallable("https://img1.baidu.com/it/u=1863405005,3705892836&fm=26&fmt=auto&gp=0.jpg","3.jpg");

    //建立執行服務
    ExecutorService ser = (ExecutorService) 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();

    System.out.println(rs1);
    System.out.println(rs2);
    System.out.println(rs3);

    //關閉服務
    ser.shutdownNow();
    }

    }

    //下載器
    class WebDownloader1 {
    //下載方法
    public void downloader1(String url, String name) {
    try {
    FileUtils.copyURLToFile(new URL(url), new File(name));
    } catch (IOException e) {
    e.printStackTrace();
    System.out.println("IO異常,downloader方法出現問題");
    }
    }
    }
  • 模擬龜兔賽跑

    package com.yeyue.demo01;

    //模擬龜兔賽跑
    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(1);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }

    //判斷遊戲是否結束
    boolean flag = gameOver(i);
    if(flag){
    break;
    }


    System.out.println(Thread.currentThread().getName()+"跑了"+i+"步");
    }
    }

    private boolean gameOver(int steps){
    if(winner!=null){
    return true;
    }{
    if(steps>=100){
    winner = Thread.currentThread().getName();
    System.out.println("winner is " + winner);
    return true;
    }
    }
    return false;
    }

    public static void main(String[] args) {
    Race race = new Race();

    new Thread(race,"兔子").start();
    new Thread(race,"烏龜").start();
    }

    }