|
| |||||||||
| Tags: java, java programming, java synchronization, performance, synchronize, synchronizing java |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| Synchronizing Java program for performance Java Synchronization I. Introduction : Timing is essential in java programming, therefore java uses multiple threads (that is to say in virtually all applications). Indeed, without synchronization, it is impossible to develop a robust application that works regardless of the interleaving of execution threads. It is considered that: you know the java syntax, object-oriented programming and general operation threads and the scheduler etc. I.A - General Problems: Many synchronization problems arise, these are few of them 1. Several receivers can not simultaneously make work in the line of work, otherwise the work would be on the same box. 2. If the work queue is full, the receiver must wait for a box becomes available for filing a new job. 3. Many workers can not take everyone working together, otherwise they would have the same work process. 4. If the work queue is empty, workers must wait until a job is submitted in order to treat. 5. Many workers can not simultaneously make the results of an in-line results, otherwise the results would be on the same box. 6. If the results file is full, the workers must wait for a box becomes available for filing a new result. 7. If the results file is empty, the sender must wait until a result is available. I.B - Objectives The goal is to solve these general problems, which can be in most cases be reduced. To solve them properly, we must ensure: The Safety : Nothing bad happens, whatever the interlacing of threads (two workers can never take the same job) The vivacity : Something good finally happen (if the work queue is not empty, a worker would eventually work). We must also take into account if possible, for example, two works may be filed consecutively executed sequentially by the same worker. |
|
#2
| |||
| |||
| Re: Synchronizing Java program for performance II. Mutual exclusion of critical sections Mutual exclusion can solve the problems of synchronization 1, 3 and 5 which are in the list of the general problems. Here is a sample program that does not meet the security, if multiple threads can simultaneously access: Code: class Lsttab {
private String[] t = new String[50];
private int ind = 0;
void adds (Strings)
{
t [ind] = s;
ind + +;
}
} Code: void adds (Strings)
{
tab [ind] = s; / / (a1)
ind + +; / / (a2)
} Code: void adds (Strings)
{
tab [ind] = s; / / (b1)
ind + +; / / (b2)
} To implement the mutual exclusion, we must use Locks. When a thread enters a critical section, it requests the lock. If it gets its, it can then execute the code. If it does not get it, because another thread has already taken, then it is blocked waiting to receive it. It is possible to use a potentially infinite number of locks, and then make specific mutual exclusion. II.A - Synchronized Keyword: To achieve this in Java, the easiest method is to use the keyword synchronized. Here's how it is used: Code: synchronized(Obj)
{
/ / critical section
} Code: synchronized(mlst)
{
mlst = new ArrayList <String>();
} Code: void method ()
{
synchronized(this) {
/ / critical section
}
} Code: synchronized void method ()
{
/ / critical section
} Another method for making locks, appeared in Java 1.5. At first sight it may seem complicated, but it is also more powerful in the sense that it can do things that the keyword synchronized does not. Here's how to use it: Code: Lock lc = newReentrantLock ();
lc.lock ();
try {
/ / critical section
} finally
{
lc.unlock ();
} |
|
#3
| |||
| |||
| Re: Synchronizing Java program for performance III. Cooperative Synchronization: Synchronization cooperative solves the problems of synchronization 2, 4, 6 and 7 which are listed in the General problems. III.A- Methods of the Object class Use: The most basic synchronization between multiple threads Java is the use of methods wait () and notify () (and possibly notifyAll ()) defined in class Object. To understand their operation, here is an sample code Code: class lsttab
{
private String[] t = new String[50];
private int ind = 0;
synchronized void adds (Strings) {
t [ind] = s;
ind + +;
notify ();
System.out.println ("notify () executed");
}
synchronized StringgetPremierElement () {
/ / until the list is empty
while(ind == 0) {
try {
/ / passive waiting
wait ();
} catch(InterruptedException e) {
e.printStackTrace ();
}
}
return t [0];
}
} Limits: Apart from the difficulty of implementation for complex problems, this solution reached its limits therefore a critical section must be synchronized on multiple locks. Here is am sample example showing the same: Code: class SomeClass {
private Final Object lc1 = new Object ();
private Final Object lc2 = new Object ();
void method () {
synchronized(lc1) {
synchronized(lc2) {
while(condition) {
lc2.wait ();
}
}
}
}
void notify () {
lc2.notify ();
}
} III-B. Package java.util.concurrent.locks: The solution we have seen to realize mutual exclusion using this package can also go beyond the boundaries of class methods Object. The principle is to use a single bolt, but with several condition variables (on which we can perform operations similar to wait () and notify ()). Here is an sample code for it Code: class SomeClass {
private Final Lock lc = new ReentrantLock ();
private Final Condition cd1 = lc.new Condition ();
private Final Condition cd2 = lc.new Condition ();
void method1 () throws InterruptedException {
lc.lc ();
try{
cd1.await ();
cd2.signal ();
} finally{
lc.unlc ();
}
}
void method2 () throws InterruptedException {
lc.lc ();
try{
cd1.signal ();
cd2.await ();
} finally{
lc.unlc ();
}
}
} III.C - Threads: So far, the word thread pointed to a thread. In this section, the word Thread Class mean Thread Java. The relationship between the two is that the method call start () Class Thread creates a new thread in which code is executed. This is not an article on the Thread class, but here are some ways to know: - static Thread currentThread () : Obtains the current thread. - static void yield () : A chance to let other threads run. - static void sleep (long msec) throws InterruptedException : Suspend execution of the calling thread during the specified time - void interrupt () : Causes either lift the exception InterruptedException if the activity is blocked on a synchronization operation or placement of an indicator interrupted. - void join () : Waiting blocking the termination of the thread of execution |
|
#4
| |||
| |||
| Re: Synchronizing Java program for performance IV. Solutions implemented in Java 1.5 and greater: Version 1.5 of Java and its package java.util.concurrent (and its sub-packages) provide tools for synchronizing the highest level. A useful tool is Executor. There is a waiting list for blocking action to perform. We found exactly the same mechanism when using the thread dedicated to the graphic display (EventDispatchThread) of Swing : SwingUtilities.invokeLater (Runnable) put in the queue an action to perform in the thread dedicated to the graphic display. Here is an example of the use of Executor : Code: Executors.new SingleThreadExecutor Executor executor = ();
executor.execute (newRunnable () {
Public void run () {
System.out.println ("asynchronous call 1");
}
}};
executor.execute (new Runnable () {
Public voidrun () {
System.out.println ("asynchronous call 2");
}
}}; V. Inter-lock The most common problem in synchronization during development is the Inter-lock. An inter-locking (or deadlock) is a deadlock that occurs. Here is a simple example of inter-locking: Code: class DeadLock {
private Final Object lc1 = new Object ();
private Final Object lc2 = new Object ();
void a() throwsInterruptedException {
lc1.wait ();
lc2.notify ();
}
void b() throwsInterruptedException {
lc2.wait ();
lc1.notify ();
}
} VI. Avoid unnecessary synchronization The timing is something essential in many situations. However, it is expensive in CPU, do not sync everything. If a method can be called only a single thread, do not synchronize. When you use a lot of sync, try to see if the model of a queue does not fit better (Executor), It will simplify a lot more of your code. You can avoid the Vector , Hashtable which are synchronized by default and use the ArrayList , HashMap which are not synchronized by default and best for performance. |
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "Synchronizing Java program for performance" | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Dr. Java program | bamggut29 | Software Development | 2 | 26-11-2011 04:24 AM |
| Sluggish Program Performance -CPU fluctuations | Priscilia | Motherboard Processor & RAM | 3 | 03-03-2011 04:03 PM |
| Ping program in java | ISAIAH | Software Development | 5 | 21-01-2010 10:10 AM |
| Calendar program in java | D_chapple | Software Development | 3 | 01-12-2009 05:20 PM |
| Link List Example in Java Sample program in Java | trickson | Software Development | 2 | 04-08-2009 09:23 PM |