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
Solved

ManualResetEvent alternative in Java

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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
java stored proc example 9 44
servlet filter example 37 71
Crystal Reports Licensing Questions 4 34
Need Help! Getting a syntax error and don't understand why 3 31
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 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…

861 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