Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7703461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:27:51+00:00 2026-05-31T23:27:51+00:00

I have a problem with Observer-Pattern and deadlock using threads. package observerDeadLock; import java.util.Observable;

  • 0

I have a problem with Observer-Pattern and deadlock using threads.

package observerDeadLock;

import java.util.Observable;

public class MyModel extends Observable {
Integer foo;

public MyModel() {
    foo = 0;
}

public void changeStatus(Integer newStatus) {
    foo = newStatus;
    notifyObservers(newStatus);     
}
}

package observerDeadLock;

public class Job extends Thread {
    public MyModel model;

    public Job(MyModel model) {
        super();

        this.model = model;
    }

    public void run() {
        prepareJob();
        runMyJob();
    }

    private void runMyJob() {
        // Some stuff
        Integer choice = 1;

        if (choice == 3) {
            return;
        }
        else if (choice == 2) {
            return;
        }
        else if (choice == 1) {                                     
            model.changeStatus(123);    // Set a particalar status that MyController receive as wrong!
                    // PROBLEM: The Controller listen the changeStatus(123) of notifyObserver of MyModel and call a join() because I want the thread join and quit()
            return; // <<- NEVER EXECUTED! join(timeout) isn't the solution IHMO...s
        }       

        return;
    }

    private void prepareJob() {
        // Do some stuff
    }

}

package observerDeadLock;

import java.util.Observable;
import java.util.Observer;

public class MyController implements Observer {
    private Job myJob;
    private MyModel model;

    public MyController() {

    }

    public void startJob() {
        model = new MyModel();
        model.addObserver(this);

        myJob = new Job(model);
        myJob.start();
    }

    @Override
    public void update(Observable o, Object arg) {
        if (o instanceof MyModel) {
            if (((Integer) arg) == 1) {     
                // do other stuff
            }
            else if (((Integer) arg) == 2) {        
                // do other stuff
            }
            else if (((Integer) arg) == 123) {      // 123 => Wrong state for myController, so must stop Job
                // Stop myJob!!!
                try {
                    //myJob.join(timeout); // isn' the solution IHMO

                    myJob.join();               // PROBLEM HERE!!! In job, the "return" statment is locked in changeStatus() -> myModel.notifyobserver() that lock here in JOIN();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }           
            }


        }
    }
}

What I can do to resolve that problem? I suppose that use Thread.join() than Thread.interrupt() is a bad practice…
Suggestion?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T23:27:53+00:00Added an answer on May 31, 2026 at 11:27 pm

    You’re apparently trying to join() the current thread (you can see this for yourself if you replace myJob.join() with System.out.println( Thread.currentThread() )), which isn’t a very good idea, as the thread will be stuck forever, or until someone else interrupts it from the outside.

    Instead of join(), just call interrupt(), there’s nothing shameful about that. (Although whether it will have any effect depends on what else is in runMyJob())

    Update: I can only assume that you edited out parts of your code for brevity, but if MyController really does nothing else if it encounters the exit value 123 than to stop the updating thread, then the whole construction is unnecessary. You should simply return from the runMyJob() method without setting anything and the thread will stop gracefully.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the Observable class / Observer interface in Java to implement the observer
I have the following problem. Given a Interface EventNotifier for the Observer Pattern: public
I have met an interesting problem while implementing the Observer pattern with C++ and
I have problem creating new instance of excel 2007 using VBA (from Access 2002).
I have a problem regarding to MPMoviePlayer. I am using [self presentModalViewController:moviePlayer animated:YES]; to
I have a problem using DatagramSocket. The problem is that I can't run two
I often come accross the problem that I have a class that has a
I was having problems with class design until i found out about observable (using
I have defined the following service with an observer of messages sent. The problem
I have a strange problem in matching a pattern. Consider the Perl code below

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.