用线程交换打印#和& 用线程交换打印#和&10最后的结果应该是这样的#&#&#&#&的形式 我有sleep()方法会作 但是我想用synchronized notify() 来做 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public class MyThread { private static boolean flag = false; public static void main(String[] args) { new Thread(new ThreadA()).start(); new Thread(new ThreadB()).start(); } public synchronized void printA() throws Exception { while (true) { if (flag) wait(); System.out.println("#"); flag = true; notifyAll(); } } public synchronized void printB() throws Exception { while (true) { if (!flag) wait(); System.out.println("&"); flag = false; notifyAll(); } }}class ThreadA implements Runnable { private static MyThread myThread = new MyThread(); public void run() { try { myThread.printA(); } catch (Exception e) { } }}class ThreadB implements Runnable { private static MyThread myThread = new MyThread(); public void run() { try { myThread.printB(); } catch (Exception e) { } }}差不多是这样吧,但总是阻塞了。 线程学的不咋样,学习啊 public class MyThread { private static boolean flag = false; public static void main(String[] args) { MyThread myThread = new MyThread(); new Thread(new ThreadA(myThread)).start(); new Thread(new ThreadB(myThread)).start(); } public synchronized void printA() throws Exception { while (true) { if (flag) wait(); System.out.println("#"); flag = true; notifyAll(); } } public synchronized void printB() throws Exception { while (true) { if (!flag) wait(); System.out.println("&"); flag = false; notifyAll(); } }}class ThreadA implements Runnable { private static MyThread myThread; public ThreadA(MyThread myThread) { this.myThread = myThread; } public void run() { try { myThread.printA(); } catch (Exception e) { } }}class ThreadB implements Runnable { private static MyThread myThread = new MyThread(); public ThreadB(MyThread myThread){ this.myThread = myThread; } public void run() { try { myThread.printB(); } catch (Exception e) { } }}哈哈,改进了哈,终于可以执行了。给我分儿。 这个差不多是一个 生产与消费 的结构 JAVA中Vector怎样存放一个动态的二维数组 double与float显示的问题 continue 的问题 关闭jfreechart整个软件就跟着也关了。。。 环境变量设置问题 在线等候: 关于文件读写的问题:是否都要关闭BufferWriter and FileWriter? 请教!!!!!!!! 感谢beyond_xiruo(希偌)和FooSleeper(FooSleeper)帮助 送100分求救,JBuilder6.0环境设置问题 有谁需要考java程序员 利用JAVA将经纬度写入EXIF中 为什么synchronized 无法锁定继承了thread的对象
public class MyThread {
private static boolean flag = false; public static void main(String[] args) {
new Thread(new ThreadA()).start();
new Thread(new ThreadB()).start();
} public synchronized void printA() throws Exception {
while (true) {
if (flag)
wait();
System.out.println("#");
flag = true;
notifyAll();
}
} public synchronized void printB() throws Exception {
while (true) {
if (!flag)
wait();
System.out.println("&");
flag = false;
notifyAll();
}
}
}class ThreadA implements Runnable {
private static MyThread myThread = new MyThread(); public void run() {
try {
myThread.printA();
} catch (Exception e) {
}
}
}class ThreadB implements Runnable {
private static MyThread myThread = new MyThread(); public void run() {
try {
myThread.printB();
} catch (Exception e) {
}
}
}差不多是这样吧,但总是阻塞了。 线程学的不咋样,学习啊
public class MyThread {
private static boolean flag = false; public static void main(String[] args) {
MyThread myThread = new MyThread();
new Thread(new ThreadA(myThread)).start();
new Thread(new ThreadB(myThread)).start();
} public synchronized void printA() throws Exception {
while (true) {
if (flag)
wait();
System.out.println("#");
flag = true;
notifyAll();
}
} public synchronized void printB() throws Exception {
while (true) {
if (!flag)
wait();
System.out.println("&");
flag = false;
notifyAll();
}
}
}class ThreadA implements Runnable {
private static MyThread myThread; public ThreadA(MyThread myThread) {
this.myThread = myThread;
}
public void run() {
try {
myThread.printA();
} catch (Exception e) {
}
}
}class ThreadB implements Runnable {
private static MyThread myThread = new MyThread(); public ThreadB(MyThread myThread){
this.myThread = myThread;
}
public void run() {
try {
myThread.printB();
} catch (Exception e) {
}
}
}哈哈,改进了哈,终于可以执行了。
给我分儿。 这个差不多是一个 生产与消费 的结构