Fun java multithreading (wait and notifyAll proper use posture) Fun Java multi-threaded https://www.cnblogs.com/haibiscuit/p/11094348.html (table tennis competition)

Reproduced, please indicate the address of the blog

I blog and github account, if you help please on my github project AioSocket point of a star, motivation of community contribution

Who will knock codes, key knock out a good performance but simple and easy to understand and maintain code not every programmer can do, the key is to find a good posture, so as to avoid a lot of the pit.

Yes, today's talk is wait and notifyAll proper use posture. (Following Previous Fun Java multithreading (table tennis competition) https://www.cnblogs.com/haibiscuit/p/11094348.html )

Before posting the code, first of all speak about the logic of the rules of procedure:

        1.6 a thread, a thread of execution + 3 operations, a thread of execution 3 - operation, and to ensure the synchronization of concurrent threads requirements

        2. The operation of each thread loop n times, default 20

        3. Do not exceed + 20 operation data, performing - when the operation data can not be less than 0

 

Posted the code:

package Thread;

import java.util.concurrent.CountDownLatch;

/ **
* Notify wait method and method of writing in the entity class object
* represents the current thread to wait to exit an operating entity object, release the lock temporarily withdraw from the operation of the current thread, notify the object's method is called by another thread to release a former thread, continues to operate the object
* /
public class AwaitTest {
public static void main (String [] args)
{
a CountDownLatch CountDownLatch a CountDownLatch new new = (. 6);
numberHolder numberHolder new new numberHolder = ();

the thread = T1 new new IncreaseThread (numberHolder, CountDownLatch);
the Thread T2 = new new DecreaseThread (numberHolder, CountDownLatch);

the Thread T3 = new new IncreaseThread (numberHolder, CountDownLatch);
the Thread T4 = new new DecreaseThread (numberHolder, CountDownLatch);

the Thread T5 = new new IncreaseThread (numberHolder, CountDownLatch);
the Thread T6 = new DecreaseThread (numberHolder, countDownLatch);


t1.start();
t2.start();

t3.start();
t4.start();

t5.start();
t6.start();


{the try
CountDownLatch.await ();
System.out.println ( "end of the main thread of execution!");
} the catch (InterruptedException E) {
// the TODO Auto-Generated Block the catch
e.printStackTrace ();
}

}

}


NumberHolder class
{// ********** very important
private int number; // execute increase the number of number of times and to perform the decrease of the same here,
// otherwise it may cause the thread has been in a wait state, unless the following change while IF
Private int I; // number of times of execution flag, test class CountDownLatch

Increase the synchronized void public ()
{
the while (number 20 is ==) // current number is 10 when the object for the current thread enters a wait state
{
the try
{
this.wait (500);
}
the catch (InterruptedException E)
{
E .printStackTrace ();
}
}

// described herein can be performed has to be woken
// number and is 0
number ++;
System.out.println ( "execution count" + (I ++));
System.out.println (number);

// notify waiting threads
this.notifyAll ();
}

Decrease the synchronized void public ()
{
the while (number == 0) // number of the current object is equal to zero, entering a wait state
{
the try
{
this.wait (500);
}
the catch (InterruptedException E)
{
e.printStackTrace () ;
}

}

// described herein can be performed has to be woken
// and the number is not 0
number The;
System.out.println ( "execution count" + (I ++));
System.out.println (number);
the this. notifyAll ();
}

}
// thread class: class control entity for the following data are added
class IncreaseThread the extends the Thread
{
Private NumberHolder numberHolder;
Private a CountDownLatch CountDownLatch;

public IncreaseThread(NumberHolder numberHolder,CountDownLatch countDownLatch)
{
this.numberHolder = numberHolder;
this.countDownLatch = countDownLatch;
}

@Override
public void RUN ()
{
for (int I = 0; I <20 is; I ++)
{
// for a certain delay
the try
{
the Thread.sleep ((Long) Math.random () * 1000);
}
the catch (InterruptedException E)
{
e.printStackTrace ();
}

// perform operations increased

numberHolder.increase();
}
countDownLatch.countDown();
}

}
// thread class to complete the operation entity class
class DecreaseThread the extends the Thread
{
Private NumberHolder numberHolder;
Private a CountDownLatch CountDownLatch;

public DecreaseThread (NumberHolder numberHolder, a CountDownLatch CountDownLatch)
{
this.numberHolder = numberHolder;
this.countDownLatch = CountDownLatch;
}

@Override
public void RUN ()
{
for (int I = 0; I <20 is; I ++)
{
// for a certain delay
the try
{
the Thread.sleep ((Long) Math.random () * 1000);
}
the catch (InterruptedException E)
{
e.printStackTrace ();
}

// perform reduction operation
numberHolder.decrease ();
}
countDownLatch.countDown ();
}

}

 

to sum up:

       To 1.wait synchronized in vivo method, if further restrictions need to use while binding (if not recommended for use in combination)

       2.notifyAll () method needs to be used within the synchronized method (not recommended notify method)

 

Guess you like

Origin www.cnblogs.com/haibiscuit/p/11097578.html