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, 05:37 AM
Last Post: 05-13-2005, 06:46 AM
By CodeMage in forum Java
Last Post: 09-19-2002, 07:26 AM
By Sam Goldberg in forum Java
Last Post: 09-26-2000, 09:52 AM
By JJ in forum Enterprise
Last Post: 07-06-2000, 04:50 AM
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center