线程互斥的问题

dodo_check 2011-04-15 10:57:50
在java里,怎么样完成
对一个类对象,在多线程操作时,
多线程可以并发读,
有一个线程写的时候 互斥其它线程写和读?
...全文
147 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
a_liar 2011-04-15
  • 打赏
  • 举报
回复
进程互斥用到synchronized, 楼上的有例子了。
意思就是,当一个线程在访问的时候,要锁上,不能让别的线程再进来,为了数据安全。
大河V5 2011-04-15
  • 打赏
  • 举报
回复
简单一个例子可以参照一下。

import java.io.IOException;
import java.nio.CharBuffer;

import com.sun.java_cup.internal.Main;

class Test {
private ReadWriteLock rw=new ReadWriteLock();
private int i = 0;
private static Test test=new Test();
public int getI() {
rw.readLock();
int ii=i;
rw.readUnlock();
return ii;
}

public void setI(int i) {
rw.writeLock();
this.i = i;
rw.writeUnlock();

}

public static Test getInstance()
{
return test;
}

}

class ReadWriteLock {
// 读状态
private boolean isRead;

// 写状态
private boolean isWrite;

public synchronized void readLock() {
// 有写入时读取线程停止
while (isWrite) {
try {
System.out.println("有线程在进行写入,读取线程停止,进入等待状态");
wait();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}

System.out.println("设定锁为读取状态");
isRead = true;
}

public synchronized void readUnlock() {
System.out.println("解除读取锁");
isRead = false;
notifyAll();
}

public synchronized void writeLock() {
// 有读取时读取线程停止
while (isRead) {
try {
System.out.println("有线程在进行读取,写入线程停止,进入等待状态");
wait();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}

// 有写入时写入线程也一样要停止
while (isWrite) {
try {
System.out.println("有线程在进行写入,写入线程停止,进入等待状态");
wait();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}

System.out.println("设定锁为写入状态");
isWrite = true;
}

public synchronized void writeUnlock() {
System.out.println("解除写入锁");
isWrite = false;
notifyAll();
}
}

public class MyThread implements Runnable
{
public void run() {
Test.getInstance().setI(1);
System.out.println(Test.getInstance().getI());
Test.getInstance().setI(2);
System.out.println(Test.getInstance().getI());
Test.getInstance().setI(3);
System.out.println(Test.getInstance().getI());

}
public static void main(String[] args) {
MyThread th=new MyThread();
Thread thread1=new Thread(th);
Thread thread2=new Thread(th);
Thread thread3=new Thread(th);
thread1.start();
thread2.start();
thread3.start();
}
}

wula0010 2011-04-15
  • 打赏
  • 举报
回复
类似于表的加锁吧,大家可以一起读,但是有写的时候就锁表,大家都不能读和写,..........
周靖峰 2011-04-15
  • 打赏
  • 举报
回复
那个,第二个例子举错了,不好意思,对synchonized还不太清楚,还是第一个例子比较好理解点
周靖峰 2011-04-15
  • 打赏
  • 举报
回复
简单的互斥,用Lock作为信号量比较简单

import java.util.concurrent.locks.*;

public class Test implements Runnable
{
private Lock lock;
private String info;
private static int num = 0; //临界资源

public Test(String str, Lock l)
{
lock = l;
info = str;
}

public void changeNum()
{
lock.lock(); //以下三行是临界区
System.out.println(info + " get " + num);
num = (int) (Math.random() * 10);
System.out.println(info + " put " + num);
lock.unlock();
}

public void run()
{
while (true)
{
changeNum();
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}

public static void main(String[] args)
{
ReentrantLock lock = new ReentrantLock();
Thread t1 = new Thread(new Test("线程1", lock));
Thread t2 = new Thread(new Test("线程2", lock));
Thread t3 = new Thread(new Test("线程3", lock));
t1.start();
t2.start();
t3.start();
}
}

而synchronized这个关键字就相当于给临界区加锁和解锁,所以也可以写成下面这样

import java.util.concurrent.locks.*;

public class Test implements Runnable
{
private Lock lock;
private String info;
private static int num = 0; //临界资源

public Test(String str, Lock l)
{
lock = l;
info = str;
}

public synchronized void changeNum()
{
//lock.lock(); //以下三行是临界区
System.out.println(info + " get " + num);
num = (int) (Math.random() * 10);
System.out.println(info + " put " + num);
//lock.unlock();
}

public void run()
{
while (true)
{
changeNum();
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}

public static void main(String[] args)
{
ReentrantLock lock = new ReentrantLock();
Thread t1 = new Thread(new Test("线程1", lock));
Thread t2 = new Thread(new Test("线程2", lock));
Thread t3 = new Thread(new Test("线程3", lock));
t1.start();
t2.start();
t3.start();
}
}

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧