【Thread】スレッドを作成する2つの方法


(一)Thread類
1.構造
java.lang.Object
  |---java.lang.Thread
2.スレッドを作成する2つの方法
(1)クラスをThreadとして宣言するサブクラスであり、このサブクラスはThreadクラスのrunメソッドを書き換えるべきである
class PrimeThread extends Thread {
         long minPrime;
         PrimeThread(long minPrime) {
             this.minPrime = minPrime;
         }
 
         public void run() {
             // compute primes larger than minPrime
              . . .
         }
}
PrimeThread p = new PrimeThread(143);
p.start();

(2)スレッドを作成する別の方法は,Runnableインタフェースを実装するクラスを宣言し,runメソッドを実装することである.次にクラスのインスタンスを割り当て、Threadの作成時にパラメータとして渡して起動できます.
(3)この方法は継承されたクラスを実現し,マルチスレッドを実現する拡張方法を提供し,インタフェースを実現する.
class PrimeRun implements Runnable {
         long minPrime;
         PrimeRun(long minPrime) {
             this.minPrime = minPrime;
         }
 
         public void run() {
             // compute primes larger than minPrime
              . . .
         }
}
PrimeRun p = new PrimeRun(143);
new Thread(p).start();

コード1:スレッドを作成する最初の方法
MyThreadクラス
// 1.  Thread 
public class MyThread extends Thread{
	
	private String threadName;
	
	public MyThread() {
		super();
	}
	//             ,                       
	public MyThread(String threadName) {
		super(threadName);
		this.threadName = threadName;
	}

	// 2.  run  
	@Override
	public void run() {
		for(int i=0;i<5;i++) {
			System.out.println(this.getName()+": hello : "+i);
		}
	}
}

Main
public class Main {

	public static void main(String[] args) {
		//no_parameter_construction_method();
		threadName_construction_method();
	}
	
	/**
	 * 1.                       
	 * */
	public static void no_parameter_construction_method() {
		// 3.         
		MyThread my1=new MyThread();
		MyThread my2=new MyThread();
		// 4.         ,       JVM      (Thread-N)
		my1.setName("  1");
		my2.setName("  2");
		// 5.    
		//   run()  ,          
		//         start()  
		my1.start();
		my2.start();
		System.out.println("end");
	}
	
	public static void threadName_construction_method() {
		// 3.         
		MyThread my3=new MyThread("  3");
		MyThread my4=new MyThread("  4");
		// 4.    
		my3.start();
		my4.start();
		System.out.println("end");
	}
}

コード1:スレッドの作成方法2
OurThreadクラス
// 1.  Runnable  
public class OurThread implements Runnable{

	// 2.  run  
	@Override
	public void run() {
		for(int i=0;i<10;i++) {
			// 3.        
			System.out.println(Thread.currentThread().getName()+":    : "+i);
		}
	}
}

Main
public class Main {

	public static void main(String[] args) {
		//  2     ,            ,        ,          
		createThread();
		createNameThread();
	}
	
	public static void createThread() {
		// 4.         
		// 5.    
		OurThread ot1=new OurThread();
		Thread t1=new Thread(ot1);
		
		OurThread ot2=new OurThread();
		Thread t2=new Thread(ot2);
		t1.start();
		t2.start();
		System.out.println("end");
	}
	
	public static void createNameThread() {
		// 4.         
		// 5.    ,     ,     JVM       
		OurThread ot1=new OurThread();
		Thread t1=new Thread(ot1,"  01");
		
		OurThread ot2=new OurThread();
		Thread t2=new Thread(ot2,"  02");
		t1.start();
		t2.start();
		System.out.println("end");
	}
}