• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java thread, snychronization...

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there is a "Threads and Synchronization" forum here...
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic