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 8370483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:55:20+00:00 2026-06-09T13:55:20+00:00

I’m trying to learn the basics of java concurrency, I start with oracle java

  • 0

I’m trying to learn the basics of java concurrency, I start with oracle java tutorial until http://docs.oracle.com/javase/tutorial/essential/concurrency/deadlock.html, I just add some line to the code example,but when I run the code, the main method never return.
I know that both of alphonseThread and gastonThread are deadlocked by each ether, but why can’t interrupt them from the main Thread?

public class DeadLock {

     static class Freind {

          private final String name;
          public Freind(String name_) {
               this.name = name_;
          }

          public String getName() {
               return this.name;
          }

          public synchronized void bow(Freind bower) throws InterruptedException {
                System.out.format("%s : %s " + "has bowed to me!%n",
                      this.name, bower.getName());
                Thread.sleep(2000);
                bower.bowBack(this);

         }

         public synchronized void bowBack(Freind bower) {
               System.out.format("%s : %s " + "has bowed back to me!%n",
                this.name, bower.getName());
         }
     }

    @SuppressWarnings("SleepWhileInLoop")
    public static void main(String[] args) throws InterruptedException {
        // wait for 4 seconds before send a interuption signal
        int patient = 4000;
        int waitingUnit = 1000;
        long start;
        final Freind alphonse = new Freind("Alphonse");
        final Freind gaston = new Freind("Gaston");
        // the first thread 
        Thread alphonseThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    alphonse.bow(gaston);
                } catch (InterruptedException ex) {
                    System.out.println("Alphnso have been killed");
                }
             }
        });
        // the second thread
        Thread gastonThread = new Thread(new Runnable() {
            @Override
            public void run() {
                 try {
                     gaston.bow(alphonse);
                 } catch (InterruptedException ex) {
                     System.out.println("Gaston have been killed ");
                 }
             }
         });
         // start the tow threads                
         start = System.currentTimeMillis();
         alphonseThread.start();
         gastonThread.start();
         while (alphonseThread.isAlive() || gastonThread.isAlive()) {
             if (((System.currentTimeMillis() - start) < patient)
                     && (alphonseThread.isAlive() || gastonThread.isAlive())) {
                 System.out.println(System.currentTimeMillis() - start);
                 System.out.println("still waiting !!");
                 Thread.sleep(waitingUnit);
              } else {
                 if (alphonseThread.isAlive()) {
                     alphonseThread.interrupt();
                     alphonseThread.join();
                 }
                 if (gastonThread.isAlive()) {
                     gastonThread.interrupt();
                     gastonThread.join();
                }                     
             }
         }
         System.out.println("finnaly I kill all of them");
     }
 }
  • 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-06-09T13:55:21+00:00Added an answer on June 9, 2026 at 1:55 pm

    but why can’t interrupt them from the main Thread?

    You can interrupt from the main thread (or anywhere). Just call Thread.interrupt() on the thread you want to interrupt.

    The problem is that interrupt won’t unblock a thread that is trying to acquire a Java primitive lock / mutex. In fact, I don’t think there is anything that will safely unblock threads that are deadlocked on primitive locks.

    If you want your application to be able to recover from deadlocks involving object locks, then you should probably be using Lock objects, and the Lock.lockInterruptibly() method … which will unblock when the thread is interrupted.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Specifically, suppose I start with the string string =hello \'i am \' me And
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

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.