Link to home
Start Free TrialLog in
Avatar of Yap_YokeWan
Yap_YokeWan

asked on

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

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.

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Different programs run in separate VMs (unless arranged explicitly not to)
Avatar of sciuriware
sciuriware

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!
If the DB server is configured to do some synchronization, then it would be one way for you to achieve this.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Yap_YokeWan

ASKER

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?
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!
For seperate programs it is the databases job to handle the concuurent access.
Yes, that's what the DB server is designed for.
(concurrency control)

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.



>>B have to wait A to finish perform task

Why?
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.
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!
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()
    {
         ...
    }
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...?

have a seperate class to handle access to each database and synchronise the methods that access the database.
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;
}
Use synchronized access to a method in a class that contains n connection pools, where n is the number of databases