简单一个例子可以参照一下。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();
简单的互斥,用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(); } }
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();
}
}
意思就是,当一个线程在访问的时候,要锁上,不能让别的线程再进来,为了数据安全。
{
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();
}
}