Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2006-04-06
21
Medium Priority
?
241 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.

0
Comment
Question by:Yap_YokeWan
  • 4
  • 3
  • 3
  • +4
21 Comments
 
LVL 92

Accepted Solution

by:
objects earned 800 total points
ID: 16389573
if its 2 different programs then there is no synchronisation between them. Only threads oin the same program.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16389575
Different programs run in separate VMs (unless arranged explicitly not to)
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 16389755
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 24

Expert Comment

by:sciuriware
ID: 16389767
If you want to access a database from 2 or more programs: database software already provides some synchronisation.

;JOOP!
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16389795
If the DB server is configured to do some synchronization, then it would be one way for you to achieve this.
0
 
LVL 12

Assisted Solution

by:Giant2
Giant2 earned 200 total points
ID: 16390047
>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
 

Author Comment

by:Yap_YokeWan
ID: 16390103
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
 
LVL 12

Expert Comment

by:Giant2
ID: 16390113
As I told before. If you have a unique server side you can syncronize all there.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 16390116
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
 
LVL 92

Expert Comment

by:objects
ID: 16390149
For seperate programs it is the databases job to handle the concuurent access.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16390172
Yes, that's what the DB server is designed for.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16390173
(concurrency control)
0
 

Author Comment

by:Yap_YokeWan
ID: 16390272

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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16390286
>>B have to wait A to finish perform task

Why?
0
 
LVL 92

Expert Comment

by:objects
ID: 16390288
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
 
LVL 12

Expert Comment

by:Giant2
ID: 16390294
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
 
LVL 13

Expert Comment

by:Webstorm
ID: 16390567
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
 

Author Comment

by:Yap_YokeWan
ID: 16397446
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
 
LVL 92

Expert Comment

by:objects
ID: 16397750
have a seperate class to handle access to each database and synchronise the methods that access the database.
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 16398789
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16398812
Use synchronized access to a method in a class that contains n connection pools, where n is the number of databases
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn different types of Android Layout and some basics of an Android App.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month11 days, 8 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question