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?
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.
By letianrong in forum Java
Last Post: 09-19-2005, 06:37 AM
Last Post: 05-13-2005, 07:46 AM
By CodeMage in forum Java
Last Post: 09-19-2002, 08:26 AM
By Sam Goldberg in forum Java
Last Post: 09-26-2000, 10:52 AM
By JJ in forum Enterprise
Last Post: 07-06-2000, 05:50 AM
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL