请教一个java中的同步问题 如果有三块内存a b c,一个线程writer循环有次序的往这三块内存中些数据,a->b->c->a->b->c...,然后还有一个线程reader也是有次序地从这三块内存中取数据,这个怎么实现? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 package XueLer;public class TestThreadABC implements Runnable{ public static SynchronzedLock lock = new SynchronzedLock(); public static char ='A'; public char name; public TestThreadABC(char name){ this.name = name; } public char getName(){ return name; } public void run() { while(SynchronzedLock.count < SynchronzedLock.total){ synchronized (lock){ if(this.getName() == ){ SynchronzedLock.count ++; System.out.println(this.toString()); = (char)(((-'A')+1)%3+'A'); } } } } public String toString(){ return "Name : " + name +" count : " + SynchronzedLock.count; } public static void main(String [] args) throws InterruptedException{ Thread thread1 , thread2 , thread3; thread1 = new Thread(new TestThreadABC('A')); thread2 = new Thread(new TestThreadABC('B')); thread3 = new Thread(new TestThreadABC('C')); thread1.start(); thread2.start(); thread3.start(); } }class SynchronzedLock{ public static final int total = 100; public static int count = 0; } Synchronze 楼上方法楼主试了没 谁会GUI编程啊?求教 编程问题,求解小弟感激不尽。 setLookAndFeel如何设置它的参数 为什么我把JButton添加到一个Jpanel中,缺显示不出来呢 [求助]关于System.out.flush(); JCreate Pro编译后不能正确执行时的问题? 谁有用java编写的局域网QQ程序 JDOM的问题 如何优化DB,有几种可行的方法,请指教 JBUILDER 这个程序有什么问题吗? 一个关于java awt中监听器的问题
package XueLer;public class TestThreadABC implements Runnable{
public static SynchronzedLock lock = new SynchronzedLock();
public static char ='A';
public char name;
public TestThreadABC(char name){
this.name = name;
}
public char getName(){
return name;
} public void run() {
while(SynchronzedLock.count < SynchronzedLock.total){
synchronized (lock){
if(this.getName() == ){
SynchronzedLock.count ++;
System.out.println(this.toString());
= (char)(((-'A')+1)%3+'A');
}
}
}
}
public String toString(){
return "Name : " + name +" count : " + SynchronzedLock.count;
}
public static void main(String [] args) throws InterruptedException{
Thread thread1 , thread2 , thread3;
thread1 = new Thread(new TestThreadABC('A'));
thread2 = new Thread(new TestThreadABC('B'));
thread3 = new Thread(new TestThreadABC('C'));
thread1.start();
thread2.start();
thread3.start();
}
}
class SynchronzedLock{ public static final int total = 100;
public static int count = 0;
}
楼上方法楼主试了没