1. 程式人生 > >(CSDN遷移) JAVA多執行緒實現-實現Runnable介面

(CSDN遷移) JAVA多執行緒實現-實現Runnable介面

  1. 實現Runnable介面 
implements Runnable
  1. 重寫run()方法
@Override
public void run(){//TODO}
  1. 建立執行緒物件:
Thread thread1 = new Thread(new ImplementsRunnable());
  1. 開啟執行緒執行:
thread1.start();
public class ImplementsRunnable implements Runnable{
    public static int num = 0;

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName() + ": num = " + num++);
        }
    }
    public static void main(String[] args) {
        Thread thread1 = new Thread(new ImplementsRunnable());
        Thread thread2 = new Thread(new ImplementsRunnable());
        thread1.setName("執行緒1");
        thread2.setName("執行緒2");
        thread1.start();
        thread2.start();
    }
}