We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Hi, Java beginner need help....Java Threads, Java Synchronization

Yap_YokeWan
Yap_YokeWan asked
on
Medium Priority
267 Views
Last Modified: 2012-06-27
Hi,

I am a Java beginner that is currently studying Java Thread, and Java Synchronization.

From What I know,

If 1 people using Program A, and calling Thread 1 and use synchronized method "abc". And another person using the same program A, so the method will synchronize and block another thread from performing task.(use data in same  database)

what happen if

1 people using program A calling Thread 1 and use method "abc", and another person using Program B calling Thread 2 BUT sharing the method of thread 1, and both of this program is accessing different database.

so it is 2 different programs, 2 threads but 1 method and 2 different database.
This 2 programs will wait for each other to perform task? because there are from different program and accessing different database BUT sharing same synchronized method.

Please Help....

Thank You.

Comment
Watch Question

Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
if its 2 different programs then there is no synchronisation between them. Only threads oin the same program.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT
Top Expert 2016

Commented:
Different programs run in separate VMs (unless arranged explicitly not to)
A Thread is an independent 'trail' of CPU activity inside a program.
A simple JAVA program has already 5-8 threads, of which one is started from your 'main()'.
Synchronisation is the co-operation/exclusion of thread-activity inside ONE program.
If you want to sync between multiple programs, you need file-locks, sockets or worse.

;JOOP!
If you want to access a database from 2 or more programs: database software already provides some synchronisation.

;JOOP!
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
If the DB server is configured to do some synchronization, then it would be one way for you to achieve this.
Commented:
>If 1 people using Program A, and calling Thread 1 ... And another person using the same program A, ...
machine 1 (or VM1)
program AA

machine 2 (or VM2)
program AA

If the syncro is done in the program AA you cannot do anything. If the 2 program call a server side like program (it's unique in all the system) they can syncronize.

Bye, Giant.

Author

Commented:
Thanks alot!

But, how about if 1 program calling 2 threads and using 1 method to access 5 database?

There are 5 companies( using same set of program thats store in 1 server) access to each of their database, will it synchronize?
 OR I have to prepare 5 set of programs for each database?

Commented:
As I told before. If you have a unique server side you can syncronize all there.
Solution 1) 5 client programs accessing 1 server
Solution 2) 5 programs accessing one database.

With 5 users threads are out of the question here.

;JOOP!
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
For seperate programs it is the databases job to handle the concuurent access.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Yes, that's what the DB server is designed for.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
(concurrency control)

Author

Commented:

how about if......

user A , B , C and D using same program, but redirect to different database.

**based on my understanding, no matter how many user using the same program and redirect to different database, it will also do synchronization.

example; user A go to database 1
              user B go to databse 1
              user C go to databse 2
              user D go to databse 1 (flow in sequence)
So, B have to wait A to finish perform task then only will run. BUT after B finished, it will run user C request, and User D have to wait although both of them is accessing to different databse.

So, is there any solution to identify IF the users' redirect to same database only we do synchronization, IF Different database, we run it concurrently.



CERTIFIED EXPERT
Top Expert 2016

Commented:
>>B have to wait A to finish perform task

Why?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
synchronisation of the threads really has nothing to do with the database, you synchronise a block of code. What is done in that block of code is irrelevent. Only one thread can execute a synchonised block at once, all other thrads must wait till the executing thread exists the synched block.

Commented:
Yap_YokeWan,
if the user A is on another Machine or use another VM no syncronization could be done client side. The only possible is server side!
Top Expert 2004

Commented:
Hi Yap_YokeWan,

Synchronization is done using lock. Each Java object can be used as a lock :
    void method()
    {
         ...
         synchronized(obj)
         {
              ...
         }
         ...
    }

if the whole method code use 'this' as the lock then
    void method()
    {
         synchronized(this)
         {
              ...
         }
    }
is equivalent to
    synchronized void method()
    {
         ...
    }

Author

Commented:
Okay, Now I know what is Java Synchro...Thank Everybody!

I am thinking if I want to do Synchro and multithread in my program how to go about it....

I want to synchro when users using same database.
I want to do multithread if users using different database.
**All user access using same set of program.

I use thread to do synchro when users perform same task and update same databse.
But my thread also redirect to specific user to specific database,but they are waiting for each other because of synchro....but no point to synchro if accessing to different databse although you sharing same thread.

Any advice...?

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
have a seperate class to handle access to each database and synchronise the methods that access the database.
Top Expert 2004

Commented:
Follow object's advice, and to get a database handle class from its name, you can use a static HashMap :

private static HashMap hm_db=new HashMap();
public static synchronized DBHandle getDatabaseHandle(String name)
{
    DBHandle db=(DBHandle )hm_db.get(name);
    if (db==null) hm_db.put(db=new DBHandle(name)); // create handle if not exist
    return db;
}
CERTIFIED EXPERT
Top Expert 2016

Commented:
Use synchronized access to a method in a class that contains n connection pools, where n is the number of databases
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.