0

is it possible to change an object attribute when its thread is interrupted?

i've got a board (cell matrix) when i click on a cell i want to interrupt one thread and change the value of one of its attributes to the clicked cell

    if(!clickedCell.isSnake()) {
        for(Snake snake : board.getSnakeList()) {
            if(snake.isSelected()) {
                snake.interrupt();
            }
        }
    }

it is possible to catch the interrupt exception and

    try{
    //some code;
    } catch (InterruptedException ex){
    // how do i catch the cell's x and y here so that i can do something like
    // snake.setFinalCell(clickedCell);
    }
    finally{
        notifyAll();
    }

thanks.

4
  • 3
    What about storing the clicked cell somewhere and accessing the property in the catch part? Commented Dec 9, 2014 at 12:09
  • 2
    A thread is not an object. An object does not have "its" thread. Interrupting a thread does not suspend it. notifyAll doesn't resume it. Java doesn't even support thread suspension. Commented Dec 9, 2014 at 12:11
  • @Smutje: like a static variable? Commented Dec 9, 2014 at 12:15
  • @MarkoTopolnik: you probably already suspect (and correctly) that i'm very new to the java [and programming in general] world. thank you so much for your intervention! Commented Dec 9, 2014 at 12:18

2 Answers 2

1

Each thread gets its own instruction pointer and callstack so that the thread knows where in code it currently is.

A thread does not get its own memory. All threads share the same memory, which means you can access the same object from 2 threads. If you think in pointers, both threads can have a pointer to the same memory address.

If you just try that, you'll find that this works fine for a long time. But then, bugs will occur. The reason is that one thread may be reading from the object while another thread is writing to the object (or both are writing). To avoid that situation, you can tell Java that only one thread can access an object at the same time

synchronized(lockObject) {
   // Access shared variables and other shared resources
}

If your example, you could do the following (pseudo-code):

class CellSynchronizer {
    Object lockObject = new Object();
    Cell currentCell;

    void setCurrentCell(Cell cell) {
        synchronized (lockObject) {
            currentCell=cell;
        }
    }

    boolean isSelected()  {
        synchronized (lockObject) {
            return currentCell.isSelected();
        }
    }

 }
Sign up to request clarification or add additional context in comments.

Comments

0

You can catch interrupted exception, but this is not recommended way to do inter-thread communication. You should synchronize the access to resources using wait() and notify().

6 Comments

wait and notify are discouraged and virtually deprecated, beginners should definitely not be directed to them. The concurrency helpers in java.util.concurrent are to be used instead, and even if nothing there works, there is the low-level LockSupport class, which is a one-for-one replacement for wait-notify, only with warts removed.
in the project i'm trying to finish that's exactly what we're expected to use, wait and notify! i'm using daniel liang's java programming as basic bibliography and although i love the book it doesn't help much in the multithreading subject because, like you said, wait and notify seems to be discouraged and deprecated... the book covers it very slightly.
@MarkoTopolnik please give me url about discouraging wait/notify. All I found about discouraging them was Thread.join javadoc, but its about discouraging them on Thread instances, not discouraging them in general.
Effective Java 2nd Edition, Item 69 is devoted to this subject.
Introduction to Java Programming, Ninth Edition, by Daniel Liang. Page 1154: "Prior to Java 5, thread communications were programmed using the object's built-in monitors. Locks and conditions are more powerful and flexible than the built-in monitor, so you can ignore this section. However, if you are working with legacy Java code, you may encounter Java's built-in monitor." Anyway, they expect me to use the built-in monitor...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.