Java线程学习

import java.util.*;

public class ThreadPriority{
	public static void main(String args[]){
		Thread t1 = new Thread(new T1());
		Thread t2 = new Thread(new T2());
		t1.setPriority(Thread.NORM_PRIORITY+3);
		t1.start();
		t2.start();
	}
}


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

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



Thread.join()的学习


import java.util.*;

public class ThreadTest0100 {
	public static void main(String args[]){
		// Runner1 r = new Runner1();
		
		// Thread t = new Thread(r);
		
		// t.start();
		// for(int i=0;i<100;i++){
			// System.out.println("Main Thread:"+i);
		// }
		
		// Mythread thread = new Mythread();
		// thread.start();
		// try{Thread.sleep(10000);}
		// catch(InterruptedException e){}
		// thread.interrupt();
		Mythread2 t1 = new Mythread2("abcdefg");
		t1.start();
		
		try{
			t1.join();
		}catch(InterruptedException e){}
		
		for(int i=1;i<-10;i++){
			System.out.println("i am main thread");
		}
	}
}

class Mythread2 extends Thread{
	Mythread2(String s){
		super(s);
	}
	public void run(){
		for(int i=0;i<10;i++){
			System.out.println("i am "+getName());
			try{
				sleep(1000);
			}catch(InterruptedException e){return;}
		}
	}
}
class Mythread extends Thread{
	public void run(){
		while(true){
			System.out.println("==="+ new Date()+"===");
			try{
				sleep(1000);
			}catch(InterruptedException e){
				return;
			}
		}
	}
}
// class Runner1 implements Runnable{
	// public void run(){
		// for(int i=0;i<100;i++){
			// System.out.println("Runner1 :"+i);
		// }
	// }
// }


你可能感兴趣的:(Java线程学习)