java学习笔记-线程sleep,yield和join方法

sleep方法

会让当前线程进入阻塞状态,但是不会释放锁.
(注意: 会丢弃当前剩余的时间片,立马进入阻塞状态)

/**
 * 对run方法中的for循环增加了对象锁
 */
public class MyThread1 extends Thread{

	Object o;
	
	public void run() {
		for (int i=0;i<10;i++) {
			System.out.println("MyThread1:"+i);
				
			if (i==5) {
				try {
					Thread.sleep(10000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
	}
}

public class MyThread2 extends Thread{

	Object o;
	
	public void run() {
		for (int i=0;i<10;i++) {
			System.out.println("MyThread2:"+i);
		}
	}
}
/**
 * 开始线程t1和t2,t1线程运行到i=5是sleep,t1线程丢弃剩余时间片
 * 立马进入阻塞状态,10s后再进入就绪状态,调度中心会重新选择新的线程
 */
public class Entrance {

	public static void main(String[] args) {

		Object o = new Object();
		
		MyThread1 t1 = new MyThread1();
		MyThread2 t2 = new MyThread2();
			
		t1.o = o;
		t2.o = o;
		
		t1.start();
		t2.start();
	}

}

运行结果:

MyThread1:0
MyThread1:1
MyThread1:2
MyThread1:3
MyThread1:4
MyThread1:5
MyThread2:0
MyThread2:1
MyThread2:2
MyThread2:3
MyThread2:4
MyThread2:5
MyThread2:6
MyThread2:7
MyThread2:8
MyThread2:9
MyThread1:6
MyThread1:7
MyThread1:8
MyThread1:9

不会释放锁

public class MyThread1 extends Thread{

	Object o;
	
	public void run() {
		synchronized(o) {
			for (int i=0;i<10;i++) {
				System.out.println("MyThread1:"+i);
				
				if (i==5) {
					try {
						Thread.sleep(10000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
		
	}
}
public class MyThread2 extends Thread{

	Object o;
	
	public void run() {
		synchronized(o) {
			for (int i=0;i<10;i++) {
				System.out.println("MyThread2:"+i);
			}
		}
	}
}
/**
 * MyThread1增加了synchronzied对象锁,MyThread2没有改变
 * 过程与上面是一致的,只不过t1线程此时占用了锁,虽然进入了阻塞状态,但是并不会释放锁
 */
public class Entrance {

	public static void main(String[] args) {

		Object o = new Object();
		
		MyThread1 t1 = new MyThread1();
		MyThread2 t2 = new MyThread2();
			
		t1.o = o;
		t2.o = o;
		
		t1.start();
		t2.start();
	}

}

运行结果:
MyThread1:0
MyThread1:1
MyThread1:2
MyThread1:3
MyThread1:4
MyThread1:5
MyThread1:6
MyThread1:7
MyThread1:8
MyThread1:9
MyThread2:0
MyThread2:1
MyThread2:2
MyThread2:3
MyThread2:4
MyThread2:5
MyThread2:6
MyThread2:7
MyThread2:8
MyThread2:9

yield方法

会让当前线程暂停执行,进入就绪状态,但是不会释放锁
(注意: 会丢弃当前剩余的时间片,立马回到调度中心)
具体调度中心再选择那个线程是不清楚的

join方法

会让产生线程进入阻塞状态,直到相应线程执行完毕。
(注意: 会丢弃当前剩余的时间片,立马进入阻塞状态)
在那个线程中执行那个线程进入阻塞状态,那个线程调用的join就是那个线程执行完毕阻塞的线程才能执行

public class MyThread2 extends Thread{

	Object o;
	
	public void run() {
		for (int i=0;i<10;i++) {
			System.out.println("MyThread2:"+i);
		}
	}
}
public class Entrance {

	public static void main(String[] args) throws InterruptedException {

		MyThread2 t2 = new MyThread2();
		t2.start();
		for (int i=0;i<10;i++) {
			System.out.println("主线程:"+i);
			
			if (i==5) {
				t2.join();
			}
		}
	}
}

运行结果:
主线程:0
主线程:1
主线程:2
主线程:3
主线程:4
主线程:5
MyThread2:0
MyThread2:1
MyThread2:2
MyThread2:3
MyThread2:4
MyThread2:5
MyThread2:6
MyThread2:7
MyThread2:8
MyThread2:9
主线程:6
主线程:7
主线程:8
主线程:9

你可能感兴趣的:(java,java,多线程)