您的位置:首页 > 编程语言 > Java开发

再学java基础(11) java 线程(sleep,join,yield) 经典实例。

2012-04-07 15:53 495 查看
package com.thread;

public class TestThreadShutDown {

	/**
	 * 
	 * 友情 关闭,而不可以用 stop()这样粗暴的方法。
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		Runner4 r = new Runner4();
		Thread t = new Thread(r);
		t.start();
		
		for(int i=0;i<100000;i++) {
			if(i%10000 == 0 & i>0){
				System.out.println("in thread main i=" + i);
			}
		}
		
		System.out.println("Thread main is over");
		r.shutDown();
	}

}
class Runner4 implements Runnable{
	private boolean flag = true;
	@Override
	public void run() {
		int i = 0;
		while(flag == true){
			System.out.println("  " + i);
		}
		
	}
	
	public void shutDown(){
		flag = false;
	}
	
}



package com.thread;

/************************
 * 
 	join()方法使当前线程停下来等待,直至另一个调用join方法的线程终止。
 		值得注意的是,线程的在被激活后不一定马上就运行.
 			而是进入到可运行线程的队列中。
 		但是join()可以通过interrupt()方法打断线程的暂停状态,
 	
 		从而使线程立刻抛出InterruptedException。  
 * 
 * @author wang.dm
 *
 */
public class Test_Join_1 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Thread t = new Thread(new RunnableIm());
		t.start();
		
		try {
			// 当main 线程调用t.join 时,main 线程等待t 线程 ,等待时间是1000 
			t.join(1000); // 主线程 只等1 秒,就等待一秒钟,不管子线程什么时候结束。
			System.out.println("JOIN 结束");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}
class RunnableIm implements Runnable{

	@Override
	public void run() {
		
		try {
			System.out.println("开始睡觉……");
			Thread.sleep(9000); // T 线程睡 10秒钟。而 main 线程 只等待一秒钟。
			System.out.println("停止睡觉……");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		
	}
	
}
学习来源:http://java.chinaitlab.com/base/828720.html

package com.thread;

/*
 * 
 	
 	在main方法中 通过new ThreadTest(t).start();实例化ThreadTest 线程对象, 
	它在holdThreadLock()方法中,通过 synchronized (thread),获取线程对象t的锁,并Sleep(9000)后释放,
	这就意味着,即使
	main方法t.join(1000),等待一秒钟, 
		它必须等待ThreadTest 线程释放t锁后才能进入wait方法中,
		它实际等待时间是9000+1000 MS
	
	运行结果是:
		getObjectLock
		Begin sleep
		End sleep
		ReleaseObjectLock
		joinFinish
	
		
		
	*/
public class JoinTest {
	public static void main(String[] args) {
		
		Thread t = new Thread(new RunnableImpl());
		new ThreadTest(t).start();
		t.start();
		try {
			t.join(1000);
			System.out.println("joinFinish");
		} catch (InterruptedException e) {
			e.printStackTrace();

		}
	}
}

class ThreadTest extends Thread {

	Thread thread;

	public ThreadTest(Thread thread) {
		this.thread = thread;
	}

	@Override
	public void run() {
		holdThreadLock();
	}

	public void holdThreadLock() {
		synchronized (thread) { // 获取 线程 t 的锁。
			System.out.println("getObjectLock");
			try {
				Thread.sleep(9000);

			} catch (InterruptedException ex) {
				ex.printStackTrace();
			}
			System.out.println("ReleaseObjectLock");
		}

	}
}

class RunnableImpl implements Runnable {

	@Override
	public void run() {
		try {
			System.out.println("Begin sleep");
			Thread.sleep(2000);
			System.out.println("End sleep");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}
}
package com.thread;

import java.util.Date;

public class TestInterrupt {

	/**
	 
	 interrupt() 
		interrupt()中断线程。需要注意的是,InterruptedException是线程自己从内部抛出的,
		并不是interrupt()方法抛出的。
		对某一线程调用interrupt()时,如果该线程正在执行普通的代码.
		那么该线程根本就不会抛出InterruptedException。
		但是,一旦该线程进入到wait()/sleep()/join()后,就会立刻抛出InterruptedException。

	 * @param args
	 */
	public static void main(String[] args) {
		MyThread thread = new MyThread();
		thread.start();
		
		try {
			/**************************
			 * 
			 sleep() 
				在指定的毫秒数内让当前正在执行的线程休眠(暂停执行),
					此操作受到系统计时器和调度程序精度和准确性的影响。 

			由于sleep()方法是Thread类的方法,因此它不能改变对象的机锁。
				所以当在一个Synchronized方法中调用sleep()时,线程虽然休眠了.
					但是对象的机锁没有被释放,其他线程仍然无法访问这个对象。
			sleep()方法不需要在同步的代码块中执行。
			
			但是sleep()可以通过interrupt()方法打断线程的暂停状态,从而使线程立刻抛出InterruptedException。 
			 */
			Thread.sleep(10000);  //在哪个线程里调用 sleep方法 就让那个线程睡眠。主线程睡眠了。
		} catch (InterruptedException e) {
			thread.interrupt();
		}

	}

}

class MyThread extends Thread {
	boolean flag = true;
	public void run() {
		while(flag){
			System.out.println("====" + new Date() + "=====");
			try {
				sleep(1000);
			} catch (InterruptedException e) {
				return;
			}
		}
	}
}
package com.thread;

public class TestYield {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		MyThread3 t1 = new MyThread3("t1");
		MyThread3 t2 = new MyThread3("t2");
		t1.start();
		t2.start();
		

	}

}
class MyThread3 extends Thread {
	MyThread3(String s){
		super();
	}
	public void run(){
		for(int i=0;i<=100;i++) {
			System.out.println(getName() + " : " + i);
			if(i%10 == 0){
				//Yield()方法是停止当前线程,让同等优先权的线程运行。
				//如果没有同等优先权的线程,那么Yield()方法将不会起作用。 
				yield();
			}
		}
	}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐