紧急求助:怎样实现主线程和从线程之间的同步 问题描述: 现在需要实现主从线程能够互斥的访问一个变量,主线程写入数据,从线程读取数据。实现主写完数据后,从线程读取数据。 请各位大虾帮忙一下!多谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我用的是JBuilder里面是JDK1.4没有用java.util.concurrent.Exchanger这个类库请指教 public class Test{ public static void main(String[] args){ ArrayList<Double> a1=new ArrayList<Double>(); Semaphore s=new Semaphore(1); WriteThread w1=null; for(int i=1;i<10;i++){ w1=new WriteThread("write Thread",s,a1); w1.start(); } ReadThread r1=null; for(int i=1;i<10;i++){ r1=new ReadThread("read Thread",s,a1); r1.start(); } }}class WriteThread extends Thread{ Semaphore s=null; List<Double> l=null; public WriteThread(String name,Semaphore s,List<Double> l){ super(name); this.s=s; this.l=l; } public void run(){ try{ s.acquire(1); l.add(Math.random()); System.out.println(Thread.currentThread().getName()); s.release(1); }catch(InterruptedException e){ e.printStackTrace(); } }}class ReadThread extends Thread{ Semaphore s=null; List<Double> l=null; public ReadThread(String name,Semaphore s,List<Double> l){ super(name); this.s=s; this.l=l; } public void run(){ try{ s.acquire(1); System.out.println(Thread.currentThread().getName()+l); s.release(1); }catch(InterruptedException e){ e.printStackTrace(); } }} 一个static 变量做锁,一个static变量缓冲就行了。 线程之间同步要用到notify和wait方法 class Productor implements Runnable { private TestWaitAndNotify twn; public Productor(TestWaitAndNotify twn) { this.twn = twn; } @Override public void run() { for(int i=0; i<5; i++) { twn.writeCount(); } }}class Consumer implements Runnable { private TestWaitAndNotify twn; public Consumer(TestWaitAndNotify twn) { this.twn = twn; } @Override public void run() { for(int i=0; i<5; i++) { twn.readCount(); } } }public class TestWaitAndNotify { private int count = 1; public synchronized void writeCount() { try { wait(200); // waiting for consumer } catch (InterruptedException e1) { e1.printStackTrace(); } notify(); count += 1; System.out.println(" -->> Productor: " + count); try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } public synchronized void readCount() { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(" -->> Consumer: " + count); notify(); } public static void main(String[] args) { final TestWaitAndNotify twn = new TestWaitAndNotify(); Thread p = new Thread(new Productor(twn)); Thread c = new Thread(new Consumer(twn)); p.start(); c.start(); }}console result: -->> Productor: 2 -->> Consumer: 2 -->> Productor: 3 -->> Consumer: 3 -->> Productor: 4 -->> Consumer: 4 -->> Productor: 5 -->> Consumer: 5 -->> Productor: 6 -->> Consumer: 6 键盘事件程序出错 请教 newInstance() 关于JAVA多态的问题! 请教有关sessionListener的问题? 有没有将科学计数法转换成一般数字的方法? 比如-1.23E-3 转换为 -0.00123 今天开始重新读《Think in java》,特散分100: 一个执行是的问题!!大家捧场哦·! 用java做系统服务!高手请进! 求大神指点 将Image输出成GIF文件的方法 请问这个equals方法为什么需要做这么多次判断? 一个关于清空session属性的问题
请指教
public class Test{ public static void main(String[] args){
ArrayList<Double> a1=new ArrayList<Double>();
Semaphore s=new Semaphore(1);
WriteThread w1=null;
for(int i=1;i<10;i++){
w1=new WriteThread("write Thread",s,a1);
w1.start();
}
ReadThread r1=null;
for(int i=1;i<10;i++){
r1=new ReadThread("read Thread",s,a1);
r1.start();
}
}
}class WriteThread extends Thread{
Semaphore s=null;
List<Double> l=null;
public WriteThread(String name,Semaphore s,List<Double> l){
super(name);
this.s=s;
this.l=l;
}
public void run(){
try{
s.acquire(1);
l.add(Math.random());
System.out.println(Thread.currentThread().getName());
s.release(1);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
class ReadThread extends Thread{
Semaphore s=null;
List<Double> l=null;
public ReadThread(String name,Semaphore s,List<Double> l){
super(name);
this.s=s;
this.l=l;
}
public void run(){
try{
s.acquire(1);
System.out.println(Thread.currentThread().getName()+l);
s.release(1);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
private TestWaitAndNotify twn;
public Productor(TestWaitAndNotify twn) {
this.twn = twn;
}
@Override
public void run() {
for(int i=0; i<5; i++) {
twn.writeCount();
}
}
}class Consumer implements Runnable {
private TestWaitAndNotify twn;
public Consumer(TestWaitAndNotify twn) {
this.twn = twn;
}
@Override
public void run() {
for(int i=0; i<5; i++) {
twn.readCount();
}
}
}public class TestWaitAndNotify {
private int count = 1;
public synchronized void writeCount() {
try {
wait(200); // waiting for consumer
} catch (InterruptedException e1) {
e1.printStackTrace();
}
notify();
count += 1;
System.out.println(" -->> Productor: " + count);
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public synchronized void readCount() {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(" -->> Consumer: " + count);
notify();
}
public static void main(String[] args) {
final TestWaitAndNotify twn = new TestWaitAndNotify();
Thread p = new Thread(new Productor(twn));
Thread c = new Thread(new Consumer(twn));
p.start();
c.start();
}}console result: -->> Productor: 2
-->> Consumer: 2
-->> Productor: 3
-->> Consumer: 3
-->> Productor: 4
-->> Consumer: 4
-->> Productor: 5
-->> Consumer: 5
-->> Productor: 6
-->> Consumer: 6