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

  • Home
  • SEARCH
  • 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 8914907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:50:49+00:00 2026-06-15T04:50:49+00:00

Hello there I have slight problem and not sure where im going wrong with

  • 0

Hello there I have slight problem and not sure where im going wrong with this.

I have 2 Threads X which prints repeatedly X and Y which prints Y repeatedly. Y needs to be printed after X.

I have derived from Semaphore class BinarySemaphore:

public class BinarySemaphore extends Semaphore {

public BinarySemaphore(int initial){
    value = (initial>0) ? 1 : 0;
}

public synchronized void P() throws InterruptedException{
    while (value==0){
    wait();
    }
value = 0;
    notify();
}

public synchronized void V(){

    value = 1;
    notify();
}
}

X Thread class

public class xThread extends Thread implements Runnable{

private BinarySemaphore xSemaphore;
private BinarySemaphore ySemaphore;


public xThread(String myName, BinarySemaphore nSemaphoreX, BinarySemaphore nSemaphoreY ){
    super(myName);
    xSemaphore = nSemaphoreX;
    ySemaphore = nSemaphoreY;
}

public void run(){
    try{
    xSemaphore.P();
    System.out.println(getName());
    ySemaphore.V();
    }catch(InterruptedException E){
    System.out.println("Thread X was interrupted"); 
    }
}   
}

Y Thread Class

public class yThread extends Thread implements Runnable{

private BinarySemaphore xSemaphore;
private BinarySemaphore ySemaphore;


public yThread(String myName, BinarySemaphore nSemaphoreX, BinarySemaphore nSemaphoreY ){
    super(myName);
    xSemaphore = nSemaphoreX;
    ySemaphore = nSemaphoreY;
}

public void run(){
    try{
    ySemaphore.P();
    System.out.println(getName());
    xSemaphore.V();
    }catch(InterruptedException E){
    System.out.println("Thread Y was interrupted");
    }


}
}

When I run these threads for 10seconds all I get is

X
Y

BUILD SUCCESSFUL (total time: 10 seconds)

What am I missing here ? Why doesn’t it keep alternating them for 10 seconds ?

  • 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-15T04:50:50+00:00Added an answer on June 15, 2026 at 4:50 am

    This is because your threads are printing only once and then are terminated, like @reprogrammer suggested in his comment. You should insert a loop, something like:

    public void run(){
      while (needsToRun){
        try{
        xSemaphore.P();
        System.out.println(getName());
        ySemaphore.V();
        }catch(InterruptedException E){
        System.out.println("Thread X was interrupted"); 
        }
      }
    }   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example, I want to have code like this: class Class1 { Class2.print(Hello there.);
Hello there i'm having a problem here i have an add and edit form
Hello there android experts, I have a problem for some time and after some
Okay hello there I have 2 tables Route table which has route ID, frequency,
Hello there I will try and keep this simple and short I have a
i have a file named hello.txt which has the content Hello,there!. I want to
hello there okay so this is the deal i have my mp3 files on
Hello there okay so this is the deal i have a folder where my
I have this string @[123:peterwateber] hello there 095032sdfsdf! @[589:zzzz] I want to get 123
(This question is loosely related to my previous question ) Hello there. I have

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.