天天看點

Java線程簡單使用java 中實作多線程有四種方式單線程實作單詞抄寫繼承thread實作獨立線程單詞抄寫實作runnable接口,啟用單獨線程抄寫單詞

文章目錄

  • java 中實作多線程有四種方式
  • 單線程實作單詞抄寫
    • 1、Punishment.java
    • 2、Student.java
  • 繼承thread實作獨立線程單詞抄寫
  • 實作runnable接口,啟用單獨線程抄寫單詞

java 中實作多線程有四種方式

  1. 繼承 Thread 類
  2. 實作 Runnable 接口
  3. 使用 FutureTask
  4. 使用 Executor 架構

單線程實作單詞抄寫

1、Punishment.java

存儲要抄寫的單詞,以及剩餘的抄寫次數。主要代碼如下:

public class Punishment {
    private int leftCopyCount;
    private String wordToCopy;
    public Punishment(int leftCopyCount, String wordToCopy) {
        this.leftCopyCount = leftCopyCount;
        this.wordToCopy = wordToCopy;
    }
    public int getLeftCopyCount() {
        return leftCopyCount;
    }
    public void setLeftCopyCount(int leftCopyCount) {
        this.leftCopyCount = leftCopyCount;
    }
    public String getWordToCopy() {
        return wordToCopy;
    }
    public void setWordToCopy(String wordToCopy) {
        this.wordToCopy = wordToCopy;
    }
}
           

2、Student.java

持有 Punishment 的引用。實作了抄寫單詞的 copyWord 方法。主要代碼如下:

public class Student {
    private String name;
    private Punishment punishment;

    public Student(String name,Punishment punishment) {
        this.name=name;
        this.punishment = punishment;
    }

    public void copyWord() {
        int count = 0;
        String threadName = Thread.currentThread().getName();

        while (true) {
                if (punishment.getLeftCopyCount() > 0) {
                    int leftCopyCount = punishment.getLeftCopyCount();
                    System.out.println(threadName+"線程-"+name + "抄寫" + punishment.getWordToCopy() + "。還要抄寫" + --leftCopyCount + "次");
                    punishment.setLeftCopyCount(leftCopyCount);
                    count++;
                } else {
                    break;
                }
        }

        System.out.println(threadName+"線程-"+name + "一共抄寫了" + count + "次!");
    }
}
           
public class StudentClient {
    public static void main(String[] args) {
        Punishment punishment = new Punishment(100,"internationalization");
        Student student = new Student("小明",punishment);
        student.copyWord();
    }
}
           

輸出如下:

main線程-小明抄寫internationalization。還要抄寫99次

…(中間省略)

main線程-小明抄寫internationalization。還要抄寫0次

main線程-小明一共抄寫了100次!

繼承thread實作獨立線程單詞抄寫

//1、繼承Thread類
public class Student extends Thread {
    private String name;
    private Punishment punishment;
    public Student(String name, Punishment punishment) {
        //2、調用Thread構造方法,設定threadName
        super(name);
        this.name = name;
        this.punishment = punishment;
    }
    public void copyWord() {
        int count = 0;
        String threadName = Thread.currentThread().getName();
        while (true) {
            if (punishment.getLeftCopyCount() > 0) {
                int leftCopyCount = punishment.getLeftCopyCount();
                System.out.println(threadName + "線程-" + name + "抄寫" + punishment.getWordToCopy() + "。還要抄寫" + --leftCopyCount + "次");
                punishment.setLeftCopyCount(leftCopyCount);
                count++;
            } else {
                break;
            }
        }
        System.out.println(threadName + "線程-" + name + "一共抄寫了" + count + "次!");
    }
    //3、重寫run方法,調用copyWord完成任務
    @Override
    public void run() {
        copyWord();
    }
}
           
public class StudentClient {
    public static void main(String[] args) {
        Punishment punishment = new Punishment(100,"internationalization");
        Student student = new Student("小明",punishment);
        student.start();
    }
}
           

可以看到此時調用的不是 student 的 copyWord 方法,而是調用了 start 方法。start 方法是從 Thread 類繼承而來,調用後線程進入就緒狀态,等待 CPU 的調用。而 start 方法最終會觸發執行 run 方法,在 run 方法中 copyWord 被執行。輸出如下:

小明線程-小明抄寫internationalization。還要抄寫99次

…(中間省略)

小明線程-小明抄寫internationalization。還要抄寫0次

小明線程-小明一共抄寫了100次!

實作runnable接口,啟用單獨線程抄寫單詞

public class Student implements Runnable{
    private String name;
    private Punishment punishment;

    public Student(String name, Punishment punishment) {
        this.name=name;
        this.punishment = punishment;
    }

    public void copyWord() {
        int count = 0;
        String threadName = Thread.currentThread().getName();

        while (true) {
                if (punishment.getLeftCopyCount() > 0) {
                    int leftCopyCount = punishment.getLeftCopyCount();
                    System.out.println(threadName+"線程-"+name + "抄寫" + punishment.getWordToCopy() + "。還要抄寫" + --leftCopyCount + "次");
                    punishment.setLeftCopyCount(leftCopyCount);
                    count++;
                } else {
                    break;
                }
        }

        System.out.println(threadName+"線程-"+name + "一共抄寫了" + count + "次!");
    }

    //重寫run方法,完成任務。
    @Override
    public void run(){
        copyWord();
    }
}
           
public class StudentClient {
    public static void main(String[] args) {
        Punishment punishment = new Punishment(100,"internationalization");
        Thread xiaoming = new Thread(new Student("小明",punishment),"小明");
        xiaoming.start();
    }
}