请问Java线程同步的问题 想实现:多个线程读,多个线程写;只要有一个写线程获得资源,其他读线程和写线程都等待;而多个读线程可以同时读取,即读线程之间不需要互斥。应该怎么做?谢谢!!!!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 1,写的那个方法上面加上synchronized,这样,每次只有一个进程可以访问这个方法,以此来实现单写。读的方法就不管了,反正也不会更改数据。2,或者定一个static变量来记录要写的进程数,当这个变量大于0的时候,写进程不能进行,只有等待其它进程写完。某一正在进行的写进程结束后,检查此变量,如果为0,则唤醒正在等待的一个写进程。读进程依然不用管,还是不更新数据的原因 。 只要有一个写线程获得资源,其他读线程和写线程都等待;而多个读线程可以同时读取,即读线程之间不需要互斥。写线程锁资源。同时方法加synchronized读线程判断资源是否被锁,仅此而已,无需加锁和synchronized.有一个问题:有读线程在读,这时候来了一个写线程怎么办? 不知道好使不/* * file: ReadWriteLock.java * class: ReadWriteLock * * description: * * @author: leisore * @version: V1.0.0 */package cn.leisore.daily._2010_08_02;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantReadWriteLock;public class ReadWriteLock { public static void main(String[] args) { Cache cache = new Cache(); ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock(); ExecutorService pool = Executors.newCachedThreadPool(); // 3 writers 7 readers for (int i = 0; i < 10; i++) { if (i > 3) { pool.submit(new Reader(rwLock.readLock(), cache)); } else { pool.submit(new Writer(rwLock.writeLock(), cache)); } } try { Thread.sleep(1000 * 60); } catch (InterruptedException e) { e.printStackTrace(); } finally { pool.shutdownNow(); } } static class Cache { String cache = "init"; public String toString() { return cache; } public Cache refreshWith(String cache) { this.cache = cache; return this; } } static class Reader implements Runnable { static int counter = 0; final int id = counter++; Lock lock = null; Cache cache = null; public Reader(Lock lock, Cache cache) { this.lock = lock; this.cache = cache; } public void run() { while (!Thread.currentThread().isInterrupted()) { lock.lock(); try { Thread.sleep(200); System.out.println("Reader " + id + " read cache: " + cache.toString()); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); Thread.yield(); } } } } static class Writer implements Runnable { static int counter = 0; final int id = counter++; Lock lock = null; Cache cache = null; public Writer(Lock lock, Cache cache) { this.lock = lock; this.cache = cache; } public void run() { while (!Thread.currentThread().isInterrupted()) { lock.lock(); try { Thread.sleep(500); System.out.println("Writer " + id + " writer cache: " + cache.refreshWith("Writer " + id)); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); Thread.yield(); } } } }} 急问!!websphere上 能编译java吗? 熟悉swing的进来看看 java替换字符 谁能做出来?? 有没有中文版的JB7使用手册? 怎么连不上SOHU的POP服务器??? 寻求LDAP配置文档 从程序大本营java版来看,我发誓不买csdn的正版东西,太滥了,还不让说 Buy IBMWebsphere and Weblogic?? 短信猫在startService这不执行了,也没报错 求关于无线编程的网站 现在一般如何解析xml文件
读的方法就不管了,反正也不会更改数据。2,或者定一个static变量来记录要写的进程数,当这个变量大于0的时候,写进程不能进行,只有等待其它进程写完。某一正在进行的写进程结束后,检查此变量,如果为0,则唤醒正在等待的一个写进程。读进程依然不用管,还是不更新数据的原因 。
而多个读线程可以同时读取,即读线程之间不需要互斥。写线程锁资源。同时方法加synchronized
读线程判断资源是否被锁,仅此而已,无需加锁和synchronized.有一个问题:有读线程在读,这时候来了一个写线程怎么办?
* file: ReadWriteLock.java
* class: ReadWriteLock
*
* description:
*
* @author: leisore
* @version: V1.0.0
*/
package cn.leisore.daily._2010_08_02;import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;public class ReadWriteLock { public static void main(String[] args) { Cache cache = new Cache();
ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock(); ExecutorService pool = Executors.newCachedThreadPool(); // 3 writers 7 readers
for (int i = 0; i < 10; i++) {
if (i > 3) {
pool.submit(new Reader(rwLock.readLock(), cache));
} else {
pool.submit(new Writer(rwLock.writeLock(), cache));
}
} try {
Thread.sleep(1000 * 60);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
pool.shutdownNow();
}
} static class Cache {
String cache = "init"; public String toString() {
return cache;
} public Cache refreshWith(String cache) {
this.cache = cache;
return this;
}
} static class Reader implements Runnable { static int counter = 0;
final int id = counter++;
Lock lock = null;
Cache cache = null; public Reader(Lock lock, Cache cache) {
this.lock = lock;
this.cache = cache;
} public void run() {
while (!Thread.currentThread().isInterrupted()) {
lock.lock();
try {
Thread.sleep(200);
System.out.println("Reader " + id + " read cache: "
+ cache.toString());
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
Thread.yield();
}
}
}
} static class Writer implements Runnable { static int counter = 0;
final int id = counter++;
Lock lock = null;
Cache cache = null; public Writer(Lock lock, Cache cache) {
this.lock = lock;
this.cache = cache;
} public void run() {
while (!Thread.currentThread().isInterrupted()) {
lock.lock();
try {
Thread.sleep(500);
System.out.println("Writer " + id + " writer cache: "
+ cache.refreshWith("Writer " + id));
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
Thread.yield();
}
}
}
}
}