?
Solved

threads, locks, and concurrent processes

Posted on 2003-03-11
18
Medium Priority
?
246 Views
Last Modified: 2010-03-31
Hey guys,

Was wondering if someone could help me out with this.

I have to Threads executing simultaneously. They both have a critical section where they access an SQL database. I DON'T want both the threads to execute this critical section at the same time, because
the database connection can only be used by one process at any one time. And I am not allowed to use two separate database connections for this task.

Would appreciate some help,
Ta.
0
Comment
Question by:rich420
[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
  • 6
  • 5
  • 4
  • +1
18 Comments
 
LVL 3

Expert Comment

by:allahabad
ID: 8117394
Use wait() and notifyAll().
0
 

Author Comment

by:rich420
ID: 8117402
Sorry, I meant two Threads, instead of to Threads at the start of the 2nd paragraph
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8117430
Hi rich420,
have u read about synchronization in java .. read the basic documentation of threads from javadocs/tutorial from any tutorial of ur choice .. i can suggest one . from java.sun.com

u can synchronize the methods and objects and blocks of code
synchronization of function ( method ) means..once a thread is inside the method .. other threads cannot enter.

synchronization of object means..once a thread is accessing the object  other threads cannot enter.

same goes with synchronization of blocks ( piece of code)


Cheers!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:rich420
ID: 8117670
No, I don't know much about synchronization in Java.
Can you tell me how to synchronize a method in Java or suggest a URL.

Thanks Akshay
0
 

Expert Comment

by:Rezantis
ID: 8117716
Read up on this:

http://java.sun.com/docs/books/tutorial/essential/threads/multithreaded.html

This should help you out.  To synchronize a single method in java you add the keyword 'synchronized' to the declaration, like so:

public synchronized void randomMethod(){}

What this does is it allows one and only one thread to execute this method at a time, which sounds like what you need to force.  If the two threads are using different methods but the same object, you could instead add the synchronized keyword to the object definition in the same way.
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8117721
start from here . for threads on java ..
http://java.sun.com/docs/books/tutorial/essential/threads/index.html

here is the topic on synchronization

http://java.sun.com/docs/books/tutorial/essential/threads/multithreaded.html

btw u can download he java tutorial from java.sun.com
for many more useful topics

http://java.sun.com/docs/books/tutorial/information/download.html
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8117743
4 comments within 2 minutes .. all saying same thing :D

hey host ..btw i started writing first .. i got late in clicking
:) j/k
0
 

Expert Comment

by:Rezantis
ID: 8117770
Of course you did, of course.  :P
0
 

Author Comment

by:rich420
ID: 8124078
Can I use Semaphores?
How to use Semaphores in Java?
0
 

Author Comment

by:rich420
ID: 8124096
btw, so far I have only found Rezantis' and Akshay's comments useful. Still deciding to which of you two I should award the points.
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8124295
java doesnt provide semaphores .. but it allows u to implement .. read thru this page

http://www.javaworld.com/javaworld/javaqa/1999-11/02-qa-semaphore.html

and as for the points  u always have an option to split
0
 
LVL 8

Accepted Solution

by:
akshayxx earned 400 total points
ID: 8124316
http://www.dcs.napier.ac.uk/~shaun/rtse/labs/lab04.html

this is another good lecture article for semaphores in java
0
 

Expert Comment

by:Rezantis
ID: 8124424
You shouldn't really need semaphores, I'd have thought; the wait and notify framework is usually enough - in my admittedly limited experience.  ^^
0
 

Author Comment

by:rich420
ID: 8124801
Sorry Rezantis, I couldn't find out how to split points.
0
 

Expert Comment

by:Rezantis
ID: 8124829
Heheh.  I'll live.  :P
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8125583
if u wish i can give points Rezantis  from my account..
on ..
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
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 video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month10 days, 19 hours left to enroll

770 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