主题:关于多线程编程中锁的问题
wyjq395
[专家分:2710] 发布于 2008-05-02 13:26:00
我用java去模拟哲学家进餐问题,不知道怎么就不对。
运行产生的错误如下:
Exception in thread "Thread-1" java.lang.NullPointerException
at Philosopher.eat(UseLock.java:83)
at Philosopher.run(UseLock.java:96)
Exception in thread "Thread-3" java.lang.NullPointerException
at Philosopher.eat(UseLock.java:83)
at Philosopher.run(UseLock.java:96)
Exception in thread "Thread-0" java.lang.NullPointerException
at Philosopher.eat(UseLock.java:83)
at Philosopher.run(UseLock.java:96)
Exception in thread "Thread-2" java.lang.NullPointerException
at Philosopher.eat(UseLock.java:83)
at Philosopher.run(UseLock.java:96)
Exception in thread "Thread-4" java.lang.NullPointerException
at Philosopher.eat(UseLock.java:83)
at Philosopher.run(UseLock.java:96)
具体代码在1楼,恳请各位老师指教!
回复列表 (共3个回复)
沙发
lujiayang [专家分:0] 发布于 2008-05-02 13:27:00
/**
* @(#)UseLock.java
*使用上锁的方法管理哲学家进餐问题
*
* @
* @version 1.00 2008/5/2
*/
import java.util.Random;
final class MyLock{//自定义锁
private int locks=0;
private Thread owner=null;
public synchronized void lock(){//上锁
Thread me=Thread.currentThread();
while(locks>0&&owner!=me){
try{
wait();
}catch(InterruptedException e){}
}
owner=me;
locks++;
}
public synchronized void unlock(){//解锁
Thread me=Thread.currentThread();
if(owner!=me||locks==0){
return;
}
locks--;
if(locks==0){
owner=null;
notifyAll();
}
}
}
class Chopstick{//筷子资源作为一整体,chest中的5个变量代表5个筷子
private int[] chst={0,0,0,0,0};//0为没有分配,1为分给了边哲学家
private MyLock mutex=new MyLock();
public boolean Application(int n){//申请筷子资源
mutex.lock();//修改数据前上锁
try{
if(chst[n]==0&&chst[(n+1)%5]==0){
chst[n]=chst[(n+1)%5]=1;
mutex.unlock();
return true;
}
else {
mutex.unlock();
return false;
}
}finally{
mutex.unlock();
}
}
public void resourceReturn(int n){//归还筷子
mutex.lock();
try{
chst[n]=chst[(n+1)%5]=0;
}finally{
mutex.unlock();
}
}
}
class Philosopher extends Thread{
private int number;
private Chopstick chopstick;
public Philosopher(int num){
number=num;
start();
}
public String toString(){
return "Philosopher"+number;
}
public void think(){//哲学家在思考
System.out.println(this+" is thinking");
try{
sleep((int)(10*Math.random()));
}
catch(InterruptedException e){
//throw new RuntimeException(e);
}
}
public void eat(){//哲学家进餐
while(!chopstick.Application(number)){
//System.out.println(this+"is waiting for chopstick");
}
System.out.println(this+" is eating");
chopstick.resourceReturn(number);
}
public void run()
{
while(true)
{
think();
eat();
}
}
}
public class UseLock {
public static void main(String[] args){
Philosopher Ph0=new Philosopher(0);
Philosopher Ph1=new Philosopher(1);
Philosopher Ph2=new Philosopher(2);
Philosopher Ph3=new Philosopher(3);
Philosopher Ph4=new Philosopher(4);
}
}
板凳
nanfengren [专家分:210] 发布于 2008-05-05 08:53:00
class Philosopher extends Thread{
private int number;
private Chopstick chopstick;//未给chopstick初始化,即为null 你给它初始化试试看
public Philosopher(int num){
number=num;
start();
}
public String toString(){
return "Philosopher"+number;
}
public void think(){//哲学家在思考
System.out.println(this+" is thinking");
try{
sleep((int)(10*Math.random()));
}
catch(InterruptedException e){
//throw new RuntimeException(e);
}
}
3 楼
wyjq395 [专家分:2710] 发布于 2008-05-07 12:09:00
呵呵,的确是那错了!
以前写C++写习惯了,所以就写成那样子了!
谢谢楼上的大哥,呵呵
我来回复