여러 스 레 드 순서 로 실행 되 는 몇 가지 방법

4318 단어 Android자바
참고:http://www.cnblogs.com/icejoywoo/archive/2012/10/15/2724674.html
1.신 호 량 Sephmore
public class SyncThreadTest {

	private static Semaphore A = new Semaphore(1);
	private static Semaphore B = new Semaphore(1);
	private static Semaphore C = new Semaphore(1);
	public static void main(String[] args) {
		ThreadA a = new ThreadA();
		ThreadB b = new ThreadB();
		ThreadC c = new ThreadC();
		try {
			B.acquire();
			C.acquire();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		a.start();
		b.start();
		c.start();
		System.out.print("over
"); } static class ThreadA extends Thread { @Override public void run() { // TODO Auto-generated method stub super.run(); try { for(int i = 0; i < 10; i++) { A.acquire(); System.out.print("thread a run" + i + "
"); B.release(); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } static class ThreadB extends Thread { @Override public void run() { // TODO Auto-generated method stub super.run(); try { for(int i = 0; i < 10; i++) { B.acquire(); System.out.print("thread b run" + i + "
"); C.release(); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } static class ThreadC extends Thread { @Override public void run() { // TODO Auto-generated method stub super.run(); try { for(int i = 0; i < 10; i++) { C.acquire(); System.out.print("thread c run" + i + "
"); A.release(); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }

2、ReentrantLock 사용
3.ReetrantLock 과 Condition 의 조합
condition 도 사용 할 수 있 습 니 다.condition 의 효율 이 더 높 을 수 있 습 니 다.await 는 lock 자 물 쇠 를 방출 합 니 다.condition 의 await 와 signal 은 object 의 wait 와 notify 방법 과 유사 합 니 다.
public class SyncThreadTestReentrantLock {
	private static Lock lock = new ReentrantLock();
	private static int state = 0;
	public static void main(String[] args) {
		ThreadA a = new ThreadA();
		ThreadB b = new ThreadB();
		ThreadC c = new ThreadC();
		a.start();
		b.start();
		c.start();
		/*try {
			a.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			b.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			c.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		System.out.print("over
"); } static class ThreadA extends Thread { @Override public void run() { // TODO Auto-generated method stub super.run(); int i = 0; while(i < 10) { //System.out.print("thread a begin
"); lock.lock(); //System.out.print("thread a lock
"); if (state % 3 == 0) { System.out.print("thread a run" + "
"); state++; i++; } lock.unlock(); //System.out.print("thread a end
"); } } } static class ThreadB extends Thread { @Override public void run() { // TODO Auto-generated method stub super.run(); int i = 0; while(i < 10) { //System.out.print("thread b begin
"); lock.lock(); //System.out.print("thread b lock
"); if (state % 3 == 1) { System.out.print("thread b run" + i + "
"); state++; i++; } lock.unlock(); //System.out.print("thread b end
"); } } } static class ThreadC extends Thread { @Override public void run() { // TODO Auto-generated method stub super.run(); int i = 0; while(i < 10) { //System.out.print("thread c begin
"); lock.lock(); //System.out.print("thread c lock
"); if (state % 3 == 2) { System.out.print("thread c run" + i + "
"); state++; i++; } lock.unlock(); //System.out.print("thread c end
"); } } } }

좋은 웹페이지 즐겨찾기