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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:33:53+00:00 2026-06-09T17:33:53+00:00

Here is an exam question I scored badly in before but am trying to

  • 0

Here is an exam question I scored badly in before but am trying to get it right this time around. I would appreciate if somebody could point me in the right direction, thanks.

My questions is, is the provided solution in any way correct to solve the problem? How could it be improved.

My code follows the question.

Question:

An event object has one of two states signaled or non-signaled. When
the event is in the signaled state all or one waiting threads are
released. When it is reset to the non-signaled state all the user
threads will be forced to wait. An event stays in the signaled state
until it is reset to the non-signaled state. The basic idea is that
one or more threads can wait for some event to happen. When the event
waited for occurs the threads take whatever action is required. Using
wait/notify write an Event class.

My Attempt:

class Event  {
    boolean signal;
    String obj = new String();

    public Event(boolean signal) {
        this.signal = signal;
    }
    synchronized void getSignal(){
        while(!signal)
            try{
                wait();
            }
            catch(InterruptedException e) {}
    }
    public void setSignal(boolean signal) {
        synchronized (this) {
            this.signal = signal;
            this.notifyAll();           
        }
    }
}
  • 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-09T17:33:55+00:00Added an answer on June 9, 2026 at 5:33 pm

    I made this test class which I think answers the subject, and my Event is somehow similar to yours.

    A thread is waiting for the event to happened (when signaled), then wait for the reset while another thread is activating and deactivating the signal, from time to time.

    Edit: I wasn’t aware of your simple question so maybe this code is useless for you.
    Anyway, your Event class should work, so what can I say ?

    public class SynchronizationTest {
    
    public class Event  {
    
        boolean signal = false;
        synchronized String getEvent(){
            while(!signal) {
                try{
                    wait();
                }
                catch(InterruptedException e) {}
            }
            return "This is an event";
        }
    
        synchronized public void waitNextEvent(){
            while(signal) {
                try{
                    wait();
                }
                catch(InterruptedException e) {}
            }
        }
    
        synchronized public void setSignal(boolean signal) {
            this.signal = signal;
            this.notifyAll();
        }
    }
    
    class EventChecker extends Thread {
    
        private Event event;
        public EventChecker(Event event) {
           this.event=event;
        }
    
        public void run(){
            while (true) {
                String eventStr = event.getEvent();
                System.out.println("EventChecker: Got event '"+eventStr+"'");
                System.out.println("EventChecker: wait for new event.. \n");
                event.waitNextEvent();
            }
        }
    }
    
    class EventSignaler extends Thread {
    
        private Event event;
        public EventSignaler(Event event) {
           this.event=event;
        }
    
        public void run(){
            while (true) {
                event.setSignal(true);
                System.out.print("EventSignaler: Sending new signal\n");
                try {
                    sleep((int) (Math.random()*2200));
                } catch(Exception e){}
    
                event.setSignal(false);
                System.out.print("EventSignaler: Deactivating signal\n\n");
                try {
                    sleep((int) (Math.random()*4200));
                }
                catch(Exception e){}
           }
        }
    }
    
    public void startTest() {
    
        Event event=new Event();
           EventSignaler eventSignaler = new EventSignaler(event);
           EventChecker eventChecker=new EventChecker(event);
           eventSignaler.start();
           eventChecker.start();
    }
    
    public static void main(String arg[]) {
    
        SynchronizationTest synchro = new SynchronizationTest();
        synchro.startTest();
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing a past Function Programming exam paper and have this question: Here are
This is a past exam question focusing on arrays and here is the question:
Brief: This is a past exam question from a Miranda exam but the syntax
Here is the code in a function I'm trying to revise. This example works
I found this question in one of my previous exam papers and I am
This question is for revision purposes from a past exam paper I just want
While studying for the SCJP 6 exam, I ran into this question in a
Here's the question from a practice exam I'm taking: Given a list, x ,
Hey all here is one question from Gate exam which i found a bit
This is a question that came up in an exam paper I'm studying for.

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.