Thread(上)【015】


//、、、、、
スレッドを作成する方法1、、、、、、、、、、、、、、\

package com.testthread;

public class TestThread2 {
	public static void main(String args[]) {
		Runner2 r = new Runner2() ;
		r.start() ;
		for(int i=0; i<100; i++) {
			System.out.println("Main thread:-----" + i) ;
		}
    }
}
class Runner2 extends Thread { // Thread  
	public void run() {
		for(int i=0; i<30; i++)  {
			System.out.println("Runner2:--" + i) ;
		}
	}
}

//、、、、、
スレッドを作成する方法2、、、、、、、、、、、、、\

package com.testthread; // java.lang 
public class TestThread1 {  
	public static void main(String args[]) {
		Runner1 r = new Runner1() ; 
		Thread th = new Thread(r) ;
		th.start(); // 
		r.run();  // , 
		
		for(int i=0; i<100; i++) {
			System.out.println("Main thread:-----" +i) ;
		}
	}
}
class Runner1 implements Runnable {  //  runnable , ( ) Thread 
	public void run() {
		for(int i=0; i<30; i++) {
			System.out.println("Runner1------"+i) ;
		}
	}
}

//.....
TestInterrupted..................\\

package com.testthread;
import java.util.* ;
public class TestInterrupted {
	public static void main(String args[]) {
		Thread th = new Thread(new MyThread()) ;
		th.start();
		try {
			Thread.sleep(10000) ;  // sleep  Main  10 , th  
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		th.interrupt();  // ,MyThread  catch
	}
}
class MyThread implements Runnable {
	public void run() {
		while(true) {
			System.out.println("===" + new Date() +"===") ; // , , main 10 
			try {
				Thread.sleep(1000); // sleep  static  , 。 1 
			} catch (InterruptedException e) {
				return; //  
			}
		}
	}
}

//.....
TestJoin..................\\

package com.testthread;

public class TestJoin {
	public static void main(String[] args) {
		MyThread1 m = new MyThread1("New") ;
		m.start();
		try {
			m.join(); // m , run 
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		for(int i=1; i<=10; i++) {
			System.out.println("This is main thread.") ;
		}
	}
}
class MyThread1 extends Thread {
	MyThread1(String s) {
		super(s) ; // Thread 
	}
	
	public void run() {
		for(int i=1; i<=10; i++) {
			System.out.println("This is" + getName()); // 
			try {
				sleep(1000) ;
			} catch (InterruptedException e) {
				return ;
			}
		}
	}
}

//.....
TestPriority..................\\

package com.testthread;

public class TestPriority {
	public static void main(String args[]) {
	Thread t1 = new Thread(new MyThread3()) ;
	Thread t2 = new Thread(new MyThread4()) ;
    t1.setPriority(Thread.NORM_PRIORITY + 3) ; //Thread.NORM_PRIORITY---  
    t1.start() ;
    t2.start() ;
	}
}

class MyThread3 implements Runnable {
	public void run() {
		for(int i=0; i<100; i++) {
			System.out.println("MyThread3:  " + i) ;
		}
	}
}

class MyThread4 implements Runnable {
	public void run() {
		for(int i=0; i<100; i++) {
			System.out.println("-------Mythreads4:  " + i) ;
		}
	}
}

//.....
TestStop..................\\

package com.testthread;

public class TestStop {
	public static void main(String args[]) {
		ThreadRun tr = new ThreadRun() ;
		Thread r = new Thread(tr);
		r.start();
		for (int i = 0; i < 1000000; i++) {
			if (i % 1000 == 0) {
				System.out.println("in thread main i=" + i);
			}
		}
		System.out.println("Thread main is over");
		 tr.shutDown() ;
		r.stop();
	}
}

class ThreadRun implements Runnable {
	private boolean flag = true;

	public void run() {
		int i = 0 ;
		while (flag = true) {
			System.out.print(" " + i++);
		}
	}
	
	public void shutDown() {
		flag = false ;
	}
}


//.....
TestYield..................\\

package com.testthread;
public class TestYield {
	public static void main(String args[]) {
		MyThread2 th1 = new MyThread2("th1") ;
		MyThread2 th2 = new MyThread2("th2") ;
		th1.start();
		th2.start();
	}
}

class MyThread2 extends Thread {
	 public MyThread2(String s) {
		super(s) ;
	}
	 
	 public void run() {
		 for(int i=1; i<=100; i++) {
			 System.out.println(getName()+ ": "+i) ;
			 if(i%10 == 0) {
				 yield() ; // i 10 , , 
			 }
		 }
	 }
}