Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

synchronized, notify, wait,sleep, how to work together

Thread view: 
John_Woo - 07 Aug 2006 13:53 GMT
Hi,

let's look at the following:

    class GetConnection extends Thread
    {
          Connection  con = null;
              boolean done = false;

        public void run()
        {
            try{
                con = DriverManager.getConnection(...);
                //notifyAll();
                               done = true;
            }
            catch(Exception e){}
        }

        synchronized Connection getConnection()
        {
            return con;
        }
        synchronized boolean getDone()
        {
            return done;
        }
    }

questions:
supposed in some cases it may take up to 10-20 seconds to get the
connection while other cases just takes 1-3 seconds.
what can I code in caller method, so that
1. if in 5 seconds, interrupte GetConnection thread if it has not yet
finished;
2. preventing "done" being written while trying to interrupt this
thread.

--
Thanks
John
Toronto
dsjoblom@abo.fi - 07 Aug 2006 15:31 GMT
> Hi,
>
[quoted text clipped - 33 lines]
> 2. preventing "done" being written while trying to interrupt this
> thread.

This is a classical producer-consumer problem. Read
http://java.sun.com/docs/books/tutorial/essential/concurrency/index.html
to get familiar with the basics.

If you are using java 1.5 or later, it's easiest to implement this
using a BlockingQueue. See
http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/BlockingQueue.html

The producer thread that creates the Connection puts the connection
into the queue using the offer method, and the consumer thread that
desires to use the connection uses the poll method with desired
timeout. If the consumer doesn't get an object (poll returns null), it
can interrupt the producer thread.

You may wish to try to implement this from scratch though, to
familiarize yourself with the concepts. See the first link I posted for
more information.

Regards,
Daniel Sjöblom
Ben_ - 07 Aug 2006 15:55 GMT
Did you think to use a connection pooling library to avoid writing such code
?
John_Woo - 07 Aug 2006 16:58 GMT
> Did you think to use a connection pooling library to avoid writing such code
> ?

Yes, with localThread, in case connection dropper and can't get
re-connection soon, we have to use other connection .

Do you have more idea?
Ben_ - 07 Aug 2006 17:24 GMT
Sorry, I don't undestand your reply.

I meant to say that you could use a DataSource's Connection Pooling facility
(J2EE application) or something like Jakarta Commons DBCP (J2SE
application).

These have all parameters you seem to expect like, max time to wait to
obtain a connection, what to do if a connection is broken (purge all pool
and get all new connection or replace only the broken one), etc.

I think writing a configurable and resistant connection pool is non-trivial.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.