Solved

ManualResetEvent alternative in Java

Posted on 2006-11-29
1
1,710 Views
Last Modified: 2012-06-22
I have a Java application that communicates with a scales through TCP.
It uses two threads:

class ScalesCommunication {
   // called by thread 1
   void readLoop() {
      // connects to the scales
      // reads data sent by it
   }

   // called by thread 2
   String sendCommand(String command) {
       // sends a command and waits for its answer
   }
}

I would like to solve some synchronization problems
- sendCommand should wait until connection is built up
- after a command has been sent, the answer should be waited for

One thread is not enough, because I should deal with asynchronous messages such as keypresses. The interface is given, I can't separate building up the connection from reading incoming data.
In .NET this kind of synchronization can easily be solved by means of ManualResetEvent synchronization objects. Is there any alternative in Java?
0
Comment
Question by:pallosp
1 Comment
 
LVL 18

Accepted Solution

by:
Ravi Singh earned 250 total points
ID: 18041985
Hi, you can achieve synchronization using the java.util.concurrent.Semaphore class (use 0 permit).

http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/Semaphore.html

Example below shows you how to solve the first sync problem, the other will be similar:

import java.util.concurrent.Semaphore;

class ScalesCommunication {

   private static Semaphore sem = new Semaphore(0);

   // called by thread 1
   void readLoop() {
      //...

      //after connection established, release semaphore (value incremented by 1)
      sem.release();
   }

   // called by thread 2
   String sendCommand(String command) {

       sem.acquire(); // thread waits here if sem value == 0

       // at this point connection is established
       //...
   }
}
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now