Solved

ManualResetEvent alternative in Java

Posted on 2006-11-29
1
1,757 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
web application structure 18 77
hibernate insert example 13 29
eclipse console opening separately 2 21
Glassfish admin console not working 1 15
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

810 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