You are here:

Computer Science/Java programming question

Advertisement


Question
QUESTION: I am having a hard time trying to figure out how to lock an object. I have three classes. I have a watertreatment class, station class, and a pipe class. I've created all three of my classes. The watertreatment class is the driver class, the stations are the threads, and the pipes are the lockable object. So my question is how do I set up my pipe class so that each station object that I create can lock it and then unlock it? Right now I am creating the station and pipe objects in the watertreatmant class in a for loop.

ANSWER: Debra,

I could write a book on how this is down, but better yet I was able find you something on-line:

http://docs.oracle.com/javase/tutorial/essential/concurrency/newlocks.html

Good luck,
Jeff


---------- FOLLOW-UP ----------

QUESTION: Thank you sir. I've actually taken a look at that several times but I'm still confused on how to implement it into my program. What I am thinking is that I should have a method in my Pipe class that locks a pipe and then call that in my Station class. For instance:

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


public class Pipe()

private int number = 0;
private final Lock lock = new ReentrantLock();


   public Pipe()


      public void lock(){
         
try{
lock.lock();}finally{lock.unlock()};
}

Am I going in the right direction with that idea? Would i then be able to call a pipe object from the station class?

Answer
Debra,

Looks good except I am unsure of:

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


public class Pipe()

private int number = 0;
private final Lock lock = new ReentrantLock();


  public Pipe()


     public void lock(){
        
try{
lock.lock();}finally{lock.unlock()};
}

The 'private' aspects you may want to remove private and then try it.

Good luck,
Jeff

Computer Science

All Answers


Answers by Expert:


Ask Experts

Volunteer


Jeff Laatsch

Expertise

I can answer programming questions related to: C, C++, PHP, PERL, VB, SQL, ASP, JAVA, JS, XHTML, XML, CSS, PL/SQL, AWK, SED, VI Editor and if I don`t know the answer I have 20+ years systems experience as my resource. I can handle general questions in most areas of computer science including systems integration, API, networking related issues.

Experience

An accomplished WEB Master/Developer, Programmer Analyst, Systems Administrator with 12+ years of industry background in full system management and technical hardware/software support expertise including SUN, HP-UX, AIX, LINUX, and Windows NT,Win2000, 2003 with Active Directory: Well rounded in software engineering principles, requirements gathering, analyst, definition, system architectural design, OOA, OOD, UML, SEI-CMM and SDLC: Knowledgeable in Data Warehouse, Data Mart technologies, and experienced working with ORACLE databases utilizing ERP, SQL, PHP, PERL, C, C++, PL/SQL,SQL, JAVA, VB.NET, SOAP and other Dynamic WEB technologies: Analytical thinker and problem solver: Goal oriented professional who has a Masters in Computer Systems with emphasis in computer programming (C++ and Application Development).

Education/Credentials
City University, WA 12/03
Master of Science in Computer Systems
with Emphasis in Computer programming (C++/App Development)
Chapman University, WA 6/01
Bachelor of Science in Computer Information Systems
Emphasis in Computer networks and security

20+ years of experience in the Computer Science Industry

Awards and Honors
7/96,9/96,10/96,1/97,2/98,6/99,10/01,7/02,01/03,
Outstanding Achievement & Special Recognition

©2012 About.com, a part of The New York Times Company. All rights reserved.