Java - 同步方法无法正常工作

标签 java multithreading methods runnable synchronized

我必须编写一个程序来模拟一个游戏,其中字符串上用“*”表示的猫线程跟随用“.”表示的鼠标线程。这是我创建的同步字符串类

public class SynchronizedString {

public SynchronizedString(){
    Random rand=new Random();
    length=rand.nextInt(10)+10;  //random integer between 10 and 20
    theString=new ArrayList<Character>(); 
    for(int i=0; i<length; i++){
        theString.add(' ');
    }
    mouseIndex=rand.nextInt(length-1); //random initial position for the mouse
    theString.set(mouseIndex, '.');
    catIndex=rand.nextInt(length-1); //random initial position for the cat
    while(catIndex==mouseIndex){
        catIndex=rand.nextInt(length-1); 
    }
    theString.set(catIndex,'*');
}

public synchronized void set(int position){
    String name=Thread.currentThread().getName();

    while (occupied==true || occupiedReading==true){
        try{
            System.err.println(name+" attemped to write");
            wait(); //keep on waiting until theString is free to write
        } catch(InterruptedException e) {
            e.printStackTrace();
        } 
    } //end while
    occupied=true;
    if(name.equalsIgnoreCase("CAT"))
    {
        theString.set(catIndex,' ');
        theString.set(position,'*');
        catIndex=position;
    } 
    else if(name.equalsIgnoreCase("MOUSE")) 
    {
        theString.set(mouseIndex,' ');
        theString.set(position,'.');
        mouseIndex=position;
    }

    occupied=false;
    notifyAll();
}

public synchronized ArrayList<Character> get(){
    String name=Thread.currentThread().getName();

    while (occupied==true){
        try{
            System.err.println(name+" attemped to read");
            wait(); //keep on waiting until theString is free to write
        } catch(InterruptedException e) {
            e.printStackTrace();
        } 
    } //end while
    occupiedReading=true;

    occupiedReading=false;
    notifyAll();
    return theString;
}

public synchronized void print(){
    String name=Thread.currentThread().getName();
    while(printing)
        try {
            System.err.println(name + "attempted to print");
            wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    printing=true;

    System.out.println();
    for(Character c:theString){
        System.out.print(c.charValue());
    }
    System.out.println();

    printing=false;
    notify();
}

ArrayList<Character> theString;
private int length;
private int mouseIndex;
boolean printing;
private int catIndex;
private boolean occupied=false;
private boolean occupiedReading=false;

}

然后我有两个线程 Cat 和 Mouse 继续运行,读取同步字符串并覆盖它以更改其位置。

public class Cat extends Thread {
public Cat(SynchronizedString s){
    super("CAT");
    sharedString=s;
    catched=false;
    position=sharedString.get().indexOf('*');
}

public void run(){
    while(catched==false){
        if(position==sharedString.get().indexOf('.'));
            catched=true;
        toModify=sharedString.get();
        position=toModify.indexOf('*');
        if(position==(toModify.size()-1)){
            direction=false;
        }
        if(position==0){
            direction=true;
        }
        if(direction)
            position=position+1;
        else
            position=position-1;
        sharedString.set(position);
    }
}

boolean direction=true;
private ArrayList<Character> toModify;
boolean catched;
private int position;
private SynchronizedString sharedString;

还有鼠标线程

public class Mouse extends Thread {
public Mouse(SynchronizedString s){
    super("MOUSE");
    sharedString=s;
    catched=false;
    position=sharedString.get().indexOf('.');
}

public void run(){
    while(catched==false){
        if(position==sharedString.get().indexOf('*'));
            catched=true;
        toModify=sharedString.get();
        position=toModify.indexOf('.');
        if(position==(toModify.size()-1))
            position=position-1;
        else if(position==0)
            position=position+1;
        else{
            Random rand=new Random();
            if(rand.nextBoolean())
                position=position+1;
            else
                position=position-1;
        }
        sharedString.set(position);
    }
}

private ArrayList<Character> toModify;
private boolean catched;
private int position;
private SynchronizedString sharedString;

这是我用来打印同步字符串的显示类

public class Display extends Thread {
public Display(SynchronizedString s){
    synchronizedString=s;
    toPrint=synchronizedString.get();
}

public void run(){
    while(toPrint.indexOf('.')!=toPrint.indexOf('*'))
    {
        try {
            sleep(100);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        toPrint=synchronizedString.get();
        System.out.println();
        for(Character c:toPrint){
            System.out.print(c.charValue());
        }
        System.out.println();
    }
}
ArrayList<Character> toPrint;
private SynchronizedString synchronizedString;

这是我开始线程的地方:

public class Demo {
public static void main(String[]args){
    SynchronizedString sharedString=new SynchronizedString();

    Display display=new Display(sharedString);
    Cat cat=new Cat(sharedString);
    Mouse mouse=new Mouse(sharedString);

    display.start();
    cat.start();
    mouse.start();
}

问题是猫和老鼠都只移动一次,然后就保持在同一个位置。有人可以帮我解决这个问题吗?

最佳答案

if 语句后有一个分号:

if(position==sharedString.get().indexOf('*'));
            catched=true;

在两个线程中。因此 catched=true 立即被设置,并且线程停止工作。

关于Java - 同步方法无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19655847/

相关文章:

java - 根据高度更改 JScrollPane 滚动速度

java - 流式传输视频并在其上绘制多边形 Java

java - 打印 2 到 500 之间的素数

Python:尝试将函数参数传递给类中的另一个函数,得到 NameError:未定义名称 ' '

java - 在 Eclipse 3.3 中加速内容辅助的方法

java - 使用 Spring 的第 3 方单例类的依赖注入(inject)

java局部变量同步

java - 暂停执行 main 方法中的循环,直到所有线程完成 Java 1.5

c# - 有关线程和联接用法的基本查询C#

java - 比较调用方法的对象