151多线程(优先级&yield方法)

/*
养成习惯:数据共享用静态
*/
class Demo implements Runnable{
	public void run(){
		for(int x = 0;x < 70;x++){
			System.out.println(Thread.currentThread().toString()
			+"...."+x);
		}
	}
}
class JoinDemo{
	public static void main(String[] args)throws Exception{
		Demo d = new Demo();
		Thread t1 = new Thread(d);
		Thread t2 = new Thread(d);
		
		t1.start();
		
		t1.setPriority(Thread.MAX_PRIORITY);
		
		t2.start();
		t1.join();
		
		for(int x=0;x<80;x++){
			System.out.println("main.."+x);
		}
		System.out.println("over");
	}
}


/*
养成习惯:数据共享用静态
*/
class Demo implements Runnable{
	public void run(){
		for(int x = 0;x < 70;x++){
			System.out.println(Thread.currentThread().toString()
			+"...."+x);
			Thread.yield();
		}
	}
}
class JoinDemo{
	public static void main(String[] args)throws Exception{
		Demo d = new Demo();
		Thread t1 = new Thread(d);
		Thread t2 = new Thread(d);
		
		t1.start();
		
		//t1.setPriority(Thread.MAX_PRIORITY);
		
		t2.start();
		t1.join();
		
		for(int x=0;x<80;x++){
			System.out.println("main.."+x);
		}
		System.out.println("over");
	}
}


class ThreadTest{
	public static void main(String[] args){
		new Thread(){
			public void run(){
				for(int x=0;x<100;x++){
					System.out.println(Thread.currentThread().getName()
					+"..."+x);
				}
			}
		}.start();
		
		for(int x=0;x<100;x++){
			System.out.println(Thread.currentThread().getName()
			+"..."+x);
		}
		
		Runnable r = new Runnable(){
			public void run(){
				for(int x=0;x<100;x++){
					System.out.println(Thread.currentThread().getName()
					+"..."+x);
				}
			}
		}
		new Thread(r).start();
	}
}

你可能感兴趣的:(java)