DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2006

    Java threads, Java synchronize


    I am a beginner in java thread...some synchronization problems confusing me...

    Is it necessary for us to create a thread for each program? or single thread can be call by many programs?

    when thread call method to access to database, can we use a single thread with a method to access to many different databases? if yes, how can it snychronize it?

    please help...

    thank you...


  2. #2
    Join Date
    Feb 2006
    Cologne - Germany
    yes, you can use a single class to access resources eg databases. you even don't have to use a special thread for that issue. every programm started through java has it's own thread for running. multithreading is only neccessary, when you have to do things synchronously, eg. if you have a server, that has to work parallely with multiple clients.
    when you have multiple threads accessing a single resource, eg. a database connection or a printer, which is not multithreadable, you have to make the access to that resource synchronized:
    public synchronized void doWorkWithSingleResource(Object some parameters)...
    synchronized will tell java to lock the resource, when a thread accesses it. all other threads will have to wait, until the first thread unlocks the resource by leaving the method.
    how to access different databases depends on your demands. you can write a class, that holds multiple connections to different dbs. but you also can write one class per db. this database resource accessing classes must not have their own threads.

Similar Threads

  1. running large number of java threads
    By letianrong in forum Java
    Replies: 3
    Last Post: 09-19-2005, 05:37 AM
  2. Java vs. .Net. A questionnaire
    By Basil in forum .NET
    Replies: 1
    Last Post: 05-13-2005, 06:46 AM
  3. Replies: 2
    Last Post: 09-19-2002, 07:26 AM
  4. Replies: 0
    Last Post: 09-26-2000, 09:52 AM
  5. Re: VB vs. Visual Age for Java
    By JJ in forum Enterprise
    Replies: 1
    Last Post: 07-06-2000, 04:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.