Testing for resource management
Let's put the chestnuts for the test yesterday.
Import Java.util.concurrent.semaphore;public class Boundedbuffer<e> {Private final Semaphore availableitems, Availablespaces;private final e[] items;private int putposition = 0, takeposition = 0;public boundedbuffer (int capacity) { Availableitems = new Semaphore (0), availablespaces = new Semaphore (capacity); Items = (e[]) new object[capacity];} public Boolean isEmpty () {return availableitems.availablepermits () = = 0;} public Boolean isfull () {return availablespaces.availablepermits () = = 0;} public void put (E x) throws Interruptedexception {Availablespaces.acquire ();d oinsert (x); Availableitems.release ();} Public E take () throws Interruptedexception {Availableitems.acquire (); E item = Doextra (); Availablespaces.release (); return item;} Private synchronized void Doinsert (E x) {//TODO auto-generated method Stubint i = putposition;items[i] = X;putposition = (++i = = items.length)? 0:I;} Private synchronized E Doextra () {int i = takeposition; E x = items[i];items[i] = Null;takeposition = (++i = = Items.length)? 0:i;return x;}}For classes like Boundedbuffer, the issue of resource management is particularly important. The reason to limit the size of the cache is to prevent application failures due to resource exhaustion. Producers, for example, are much faster than consumers.
Using a heap inspection tool that measures memory usage in your application, it is easy to test out the unreasonable use of memory. This functionality is supported in many commercial and open-source heap analysis tools.
Import static Org.junit.assert.asserttrue;import Junit.framework.test;public class Puttaketest {final int capacity = 1000;class Big {double[] big = new double[10000];} public static void Main (string[] args) throws interruptedexception {new Puttaketest (). Testleak ();} void Testleak () throws interruptedexception {boundedbuffer<big> bb = new Boundedbuffer<puttaketest.big> ( capacity); Long heapsize1 = Runtime.getruntime (). TotalMemory (); for (int i = 0; i < capacity; i++) {Bb.put (New Big ());} for (int i = 0; i < capacity; i++) {Bb.take ();} Long heapsize2 = Runtime.getruntime (). TotalMemory (); System.out.println (heapsize1 + "\ n" + heapsize2);}}
The Testleak method inserts multiple large objects into a bounded cache and then removes them, and the memory usage in the 2nd heap snapshot is essentially the same as the memory usage in the first heap snapshot, however, Doextra forgets to leave the reference to the returned element empty (items[i] = NULL, the amount of memory executed in the two snapshots will be significantly different。
However, in my actual test process, two memory usage when the element's reference is set to empty the condition is still obvious difference, should be no trigger system.gc. But we can't force a garbage collection to be triggered. So there's a technical problem with this test. may also be required for the selected garbage collector. I use CMS and set the memory consumption to 75% to trigger FULLGC.
using callbacks
Import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;import Java.util.concurrent.threadfactory;import Java.util.concurrent.atomic.atomicinteger;import Javax.management.mxbean;import org.junit.experimental.max.maxcore;import static org.junit.assert.*;p Ublic class Testingthreadfactory implements Threadfactory {public final Atomicinteger numcreated = new Atomicinteger ();p rivate final Threadfactory factory = executors.defaultthreadfactory ();p ublic static void Main (string[] args) {try {new Testingthreadfactory (). Testpoolexpansion ();} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} @Overridepublic Thread Newthread (Runnable R) {//TODO auto-generated method Stubnumcreated.incrementandget (); return Factory.newthread (r);} public void Testpoolexpansion () throws interruptedexception {int max_size = 10; Testingthreadfactory threadfactory = new Testingthreadfactory (); Executorservice exec = Executors.newfixedthreadpool (max_size,threadFactory); for (int i = 0; i < ten * max_size; i++) {Exec.execute (new Runnable () {public void run () {try {thread.sleep (Lon G.max_value);} catch (Interruptedexception e) {//TODO auto-generated catch Blockthread.currentthread (). interrupt ();}});} for (int i = 0; i < && ThreadFactory.numCreated.get () < max_size; i++) {thread.sleep (100);} Assertequals (ThreadFactory.numCreated.get (), max_size); System.out.println (ThreadFactory.numCreated.get ()); Exec.shutdown ();}}The above code can test the thread pool's ability to expand. Invoke the runnable and threadfactory of the task in Threadpoolexecutor.
When you test a thread, you have the following points, creating a new thread when more threads are needed, not creating it when it is not needed, and performing a recycle operation when an idle thread needs to be reclaimed.
The process of having a thread sleep for a long time is because testing the number of tasks in the thread pool does not change over a long period of time when you advance some long-running tasks to the thread pools, so you can make some judgments, such as whether the test thread pool can scale as expected.
Test of Java Concurrent Programming (ii)