求教大神Java线程相关问题?!

aoyingzb 发布于 2014/05/30 22:01
阅读 252
收藏 1

我想制作一个java小程序让A.B两个线程(其中一个是主线程)轮流执行。代码如下:

   public class TestDemo {
public static void main(String[] args) {
final ToolMethodDemo tmd = new ToolMethodDemo();
new Thread(new Runnable() {
  @Override
       public void run() {
      for (int i = 1; i <= 50; i++) {
             tmd.threadT();    }
}
}).start();
     for (int i = 1; i <= 50; i++) {
             tmd.mainT();    }
}}


public class ToolMethodDemo {
private boolean isFlag = true;
public synchronized void mainT() {
    while (!isFlag) {
   try {
           this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
      for (int j = 1; j <= 10; j++) {
       System.out.println("main:" + j);
   }
     isFlag = false;
       this.notify();}


public synchronized void threadT() {
while (isFlag) {
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for (int j = 1; j <= 10; j++) {
System.out.println("thread:" + j);
}
isFlag = true;
this.notify();
}
}
这时可以正常实现!但是当蓝色代码写在红色前面时无法正常表现,只是main线程输出如下:“

main:1
main:2
main:3
main:4
main:5
main:6
main:7
main:8
main:9
main:10

恳求大侠指导,感激。。。

加载中
0
tab.lv
tab.lv
这样main线程wait了,肯定就不会往下面执行了。
0
aoyingzb
aoyingzb
main线程wait时释放锁,子线程获得资源,子线程为什么不会继续执行?
0
小小胖鸟
小小胖鸟

代码有点乱。。没看。写了一个轮流打印A、B、C的代码,供参考。

public class PrintABC {
	static class Print {
		Lock lock = new ReentrantLock();
		private Condition printA = lock.newCondition();
		private Condition printB = lock.newCondition();
		private Condition printC = lock.newCondition();
		private String begin = "A";

		public void printA() {
			lock.lock();
			try {
				while (!begin.equals("A")) {
					printA.await();
				}
				System.out.println("A");
				begin = "B";
				printB.signal();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				lock.unlock();
			}
		}

		public void printB() {
			lock.lock();
			try {
				while (!begin.equals("B")) {
					printB.await();
				}
				System.out.println("B");
				begin = "C";
				printC.signal();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				lock.unlock();
			}
		}

		public void printC() {
			lock.lock();
			try {
				while (!begin.equals("C")) {
					printC.await();
				}
				System.out.println("C");
				begin = "A";
				printA.signal();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				lock.unlock();
			}
		}
	}

	static class PrintA implements Runnable {
		private Print print;

		@Override
		public void run() {
			for (int i = 0; i < 10; i++) {
				print.printA();
			}
		}

		public PrintA(Print print) {
			this.print = print;
		}

		public Print getPrint() {
			return print;
		}

		public void setPrint(Print print) {
			this.print = print;
		}

	}

	static class PrintB implements Runnable {
		private Print print;

		@Override
		public void run() {
			// TODO Auto-generated method stub
			for (int i = 0; i < 10; i++) {
				print.printB();
			}
		}

		public PrintB(Print print) {
			this.print = print;
		}

		public Print getPrint() {
			return print;
		}

		public void setPrint(Print print) {
			this.print = print;
		}
	}

	static class PrintC implements Runnable {
		private Print print;

		@Override
		public void run() {
			// TODO Auto-generated method stub
			for (int i = 0; i < 10; i++) {
				print.printC();
			}
		}

		public PrintC(Print print) {
			this.print = print;
		}

		public Print getPrint() {
			return print;
		}

		public void setPrint(Print print) {
			this.print = print;
		}
	}

	public static void main(String[] args) {
		Print print = new Print();
		PrintA printA = new PrintA(print);
		PrintB printB = new PrintB(print);
		PrintC printC = new PrintC(print);
		ExecutorService threadPool = Executors.newCachedThreadPool();
		threadPool.execute(printA);
		threadPool.execute(printB);
		threadPool.execute(printC);
		threadPool.shutdown();
	}
}



0
p
ph1982

多线程不懂,请教一个问题,007行 private String begin = "A",有必须要用volatile修饰吗?

0
小小胖鸟
小小胖鸟

引用来自“ph1982”的评论

多线程不懂,请教一个问题,007行 private String begin = "A",有必须要用volatile修饰吗?

不需要。加锁能够保证原子和对其他线程可见。
0
carlos
carlos

蓝色代码先执行,则后面的代码不会执行到,因为你用的是synchronized 锁住了该线程,如果不退出,则其他线程就无法执行。那为什么不会退出了,是应为下面的代码

isFlag = false;

你设置这个标志位false.则while (!isFlag) 一直为true

所以该线程不会退出

怎么样,我找到原因了。

private boolean isFlag = true;
public synchronized void mainT() {
    while (!isFlag) {
   try {
           this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
      for (int j = 1; j <= 10; j++) {
       System.out.println("main:" + j);
   }
     isFlag = false;
       this.notify();}


返回顶部
顶部