zno2

一) Thread 两种实现方式

https://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.1

https://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.19

http://www.nakov.com/inetjava/lectures/part-1-sockets/InetJava-1.3-Multithreading.html

http://www.uml-diagrams.org/examples/java-6-thread-state-machine-diagram-example.html

 

 

Thread /θrɛd/  A thread of something such as liquid, light, or colour is a long thin line or piece of it. 

process ['prəʊses] a series of actions that are done in order to achieve a particular result.

单线程:execute a single statement or expression at a time

多线程:execute code independently at once

 

 

Java中如何创建一个Thread?

The only way for a user to create a thread is to create an object of java.lang.Thread

 

方式一:继承Thread 类 ,重写 run 方法

public class ThreadDemo {

    public static void main(String args[]) {
        MyThread thread1 = new MyThread("thread1: ");
        MyThread thread2 = new MyThread("thread2: ");
        thread1.start();
        thread2.start();
        boolean theySayThread1IsAlive = true;
        boolean theySayThread2IsAlive = true;
        do {
            if (theySayThread1IsAlive && !thread1.isAlive()) {
                theySayThread1IsAlive = false;
                System.out.println("Thread 1 is dead.");
            }
            if (theySayThread2IsAlive && !thread2.isAlive()) {
                theySayThread2IsAlive = false;
                System.out.println("Thread 2 is dead.");
            }
        } while (theySayThread1IsAlive || theySayThread2IsAlive);
    }

}

class MyThread extends Thread {
    static String message[] = { "Java", "is", "a", "programming", "language." };

    public MyThread(String id) {
        super(id);
    }

    public void run() {
        String name = getName();
        for (int i = 0; i < message.length; i++) {
            randomWait();
            System.out.println(name + message[i]);
        }
    }

    void randomWait() {
        try {
            sleep((long) (3000 * Math.random()));
        } catch (InterruptedException e) {
            System.out.println("Interrupted!");
        }
    }
}

 

thread1: Java
thread2: Java
thread1: is
thread1: a
thread2: is
thread1: programming
thread2: a
thread2: programming
thread2: language.
Thread 2 is dead.
thread1: language.
Thread 1 is dead.

 

方式二:实现 java.lang.Runnable 接口 ,然后构造Thread

public class ThreadDemo {

    public static void main(String args[]) {
        Thread thread1 = new Thread(new MyClass("thread1: "));
        Thread thread2 = new Thread(new MyClass("thread2: "));
        thread1.start();
        thread2.start();
        boolean theySayThread1IsAlive = true;
        boolean theySayThread2IsAlive = true;
        do {
            if (theySayThread1IsAlive && !thread1.isAlive()) {
                theySayThread1IsAlive = false;
                System.out.println("Thread 1 is dead.");
            }
            if (theySayThread2IsAlive && !thread2.isAlive()) {
                theySayThread2IsAlive = false;
                System.out.println("Thread 2 is dead.");
            }
        } while (theySayThread1IsAlive || theySayThread2IsAlive);
    }

}

class MyClass implements Runnable {
    static String message[] = { "Java", "is", "a", "programming", "language." };

    private String id;

    public MyClass(String id) {
        this.id = id;
    }

    public void run() {
        for (int i = 0; i < message.length; i++) {
            randomWait();
            System.out.println(this.id + message[i]);
        }
    }

    void randomWait() {
        try {
            Thread.sleep((long) (3000 * Math.random()));
        } catch (InterruptedException e) {
            System.out.println("Interrupted!");
        }
    }
}

 

thread1: Java
thread1: is
thread2: Java
thread1: a
thread1: programming
thread2: is
thread2: a
thread2: programming
thread1: language.
Thread 1 is dead.
thread2: language.
Thread 2 is dead.

 

 

 

 

 

 

 

posted on 2023-06-01 16:21  zno2  阅读(15)  评论(0编辑  收藏  举报

导航