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.

Yap_YokeWanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
if its 2 different programs then there is no synchronisation between them. Only threads oin the same program.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
Different programs run in separate VMs (unless arranged explicitly not to)
0
sciuriwareCommented:
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!
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

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

;JOOP!
0
Mayank SAssociate Director - Product EngineeringCommented:
If the DB server is configured to do some synchronization, then it would be one way for you to achieve this.
0
Giant2Commented:
>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.
0
Yap_YokeWanAuthor 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?
0
Giant2Commented:
As I told before. If you have a unique server side you can syncronize all there.
0
sciuriwareCommented:
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!
0
objectsCommented:
For seperate programs it is the databases job to handle the concuurent access.
0
Mayank SAssociate Director - Product EngineeringCommented:
Yes, that's what the DB server is designed for.
0
Mayank SAssociate Director - Product EngineeringCommented:
(concurrency control)
0
Yap_YokeWanAuthor 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.



0
CEHJCommented:
>>B have to wait A to finish perform task

Why?
0
objectsCommented:
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.
0
Giant2Commented:
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!
0
WebstormCommented:
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()
    {
         ...
    }
0
Yap_YokeWanAuthor 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...?

0
objectsCommented:
have a seperate class to handle access to each database and synchronise the methods that access the database.
0
WebstormCommented:
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;
}
0
CEHJCommented:
Use synchronized access to a method in a class that contains n connection pools, where n is the number of databases
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.