Solved

ManualResetEvent alternative in Java

Posted on 2006-11-29
1
1,826 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Is there a simpler dropbox system? 10 53
Need Help! Getting a syntax error and don't understand why 3 56
Firefox and firebub 5 29
Overriding a method 3 15
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

710 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