Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java thread, snychronization...

 
bat ken
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21398
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is a "Threads and Synchronization" forum here...
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
bat ken
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic