Search code examples
javamultithreadingmethodsrunnablesynchronized

Java - Synchronized methods not working properly


I have to do a program that simulate a game in which a cat thread represented with a '*' on a string follows a mouse thread represented with a '.'. Here's the synchronized string class I've created

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;

}

Then I have the two threads Cat and Mouse that keep running, read the synchronizedString and overwrite it changing their position.

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;

And the mouse thread

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;

Here's the display class I'm using to print the synchronized string

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;

And here's where I start the threads:

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();
}

The problem is both cat and mouse only move one time and then keep staying in the same position. Can someone help me with this issue?


Solution

  • You have a semicolon after your if statement:

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

    In both threads. So catched=true gets set right away, and threads stop working.