Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 253
  • Last Modified:

threads, locks, and concurrent processes

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
rich420
Asked:
rich420
  • 6
  • 5
  • 4
  • +1
1 Solution
 
allahabadCommented:
Use wait() and notifyAll().
0
 
rich420Author Commented:
Sorry, I meant two Threads, instead of to Threads at the start of the 2nd paragraph
0
 
akshayxxCommented:
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
Independent Software Vendors: 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!

 
rich420Author Commented:
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
 
RezantisCommented:
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
 
akshayxxCommented:
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
 
akshayxxCommented:
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
 
RezantisCommented:
Of course you did, of course.  :P
0
 
rich420Author Commented:
Can I use Semaphores?
How to use Semaphores in Java?
0
 
rich420Author Commented:
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
 
akshayxxCommented:
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
 
akshayxxCommented:
http://www.dcs.napier.ac.uk/~shaun/rtse/labs/lab04.html

this is another good lecture article for semaphores in java
0
 
RezantisCommented:
You shouldn't really need semaphores, I'd have thought; the wait and notify framework is usually enough - in my admittedly limited experience.  ^^
0
 
rich420Author Commented:
Sorry Rezantis, I couldn't find out how to split points.
0
 
RezantisCommented:
Heheh.  I'll live.  :P
0
 
akshayxxCommented:
if u wish i can give points Rezantis  from my account..
on ..
0

Featured Post

Technology Partners: 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!

  • 6
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now