jQuery in Action, 2nd edition*
The moose likes Threads and Synchronization and the fly likes Java thread, snychronization... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Java thread, snychronization..." Watch "Java thread, snychronization..." New topic
Author

Java thread, snychronization...

bat ken
Greenhorn

Joined: Dec 28, 2005
Posts: 24
HI,

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...
regards,
bk
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

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


When your Java program starts, it will create the threads necessary for the program. For Java standalone applications, one of those threads (called the main thread) will call your main() method.

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?


A thread of execution, is simply executing code. It can do whatever it wants... so yes, it can access many different databases.

As for synchronization, that is a feature used to support sharing of resources between threads... so how to "synchronize it" depends on how this thread is sharing data with other threads.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
there is a "Threads and Synchronization" forum here...
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Does every program require thread programming? Nope, but some benefit from it.

You can start up multiple threads and get a perceived performance gain when you spend time blocking on some operation, like reading or downloading files. If all your operations are CPU bound, like numeric computations, then splitting up multiple threads won't help on performance. I made a little program that downloads files from the Internet. For two to about five threads each new thread gives more throughput. After that it saturates the CPU and more threads slow things down.

You can also start up a "worker thread" separate from an AWT or Swing GUI thread so the user interface is responsive while some long running task goes on in background. Again, if the other task is CPU bound it might not give up enough CPU for the GUI thread to do much, so it won't always work as expected.

If you think you'd like to try threads, see the Sun Thread Tutorial. It's quite good.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
bat ken
Greenhorn

Joined: Dec 28, 2005
Posts: 24
Thanks Alot!!!

But how about if 1 programs that can redirect different user to different database? it will synchro right? if so, how can I specific in that program if it is sharing same databse then we do synchro, else (different databse) then we do concurrently?

OR It is not possible?since what ever program call thread also will synchro...

bat ken
Greenhorn

Joined: Dec 28, 2005
Posts: 24
Actually What I wanna do is do synchro when sharing same database, If user from same set of program access to different database, I do multithreads.
 
Don't get me started about those stupid light bulbs.
 
subject: Java thread, snychronization...