/myConcurrent/src/threadPool.java
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class threadPool implements Runnable{
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(1000);
System.out.println(System.currentTimeMillis()+" "+Thread.currentThread()+" working---");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
threadPool threadpool = new threadPool();
// ExecutorService es = Executors.newFixedThreadPool(5);
ExecutorService es = Executors.newCachedThreadPool();
for(int i=0;i<20;i++)
{
es.submit(threadpool);
}
es.shutdown();
}
}
/myConcurrent/src/future.java
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
public class future {
public static void main(String[] args) throws InterruptedException, ExecutionException {
FutureTask future = new FutureTask(new futureTest());
ExecutorService es= Executors.newSingleThreadExecutor();
es.submit(future);
System.err.println("main请求完成");
Thread.sleep(2000);
System.err.println("getting future");
System.err.println(future.get());
es.shutdown();
}
}
class futureTest implements Callable{
String para = "a";
public String call() throws Exception {
StringBuilder reStringBuilder = new StringBuilder();
// TODO Auto-generated method stub
for(int i=0;i<10;i++)
{
reStringBuilder.append(para);
reStringBuilder.append("+");
}
Thread.sleep(3000);
return reStringBuilder.toString();
}
}
/myConcurrent/src/testAllPerformance.java
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;
public class testAllPerformance {
final static int threadsnumber = 50;
final static int cyclenumber = 10000000;
static long starttime = 0;
static ArrayList threads = new ArrayList(threadsnumber);
public static void main(String[] args) throws InterruptedException {
starttime = System.currentTimeMillis();
freeTest sy = new freeTest(cyclenumber);
for(int i=0;i
{
threads.add(new Thread(sy));
threads.get(i).start();
}
for(int i=0;i
{
threads.get(i).join();
}
System.out.println("method freeTest: lasting "+(System.currentTimeMillis()-starttime)+ "ms, a="+sy.a+", " +(sy.a/(float)(cyclenumber*threadsnumber))+" remained-------");
for(int i=0;i
{
threads.remove(0);
}
// starttime = System.currentTimeMillis();
// synchronizeTest sy = new synchronizeTest(cyclenumber);
// for(int i=0;i
// {
// threads.add(new Thread(sy));
// threads.get(i).start();
// }
// for(int i=0;i
// {
// threads.get(i).join();
// }
// System.out.println("method freeTest: lasting "+(System.currentTimeMillis()-starttime)+ "ms, a="+sy.a+", " +(sy.a/(float)(cyclenumber*threadsnumber))+" remained-------");
// for(int i=0;i
// {
// threads.remove(0);
// }
// starttime = System.currentTimeMillis();
// lockTest sy = new lockTest(cyclenumber);
// for(int i=0;i
// {
// threads.add(new Thread(sy));
// threads.get(i).start();
// }
// for(int i=0;i
// {
// threads.get(i).join();
// }
// System.out.println("method freeTest: lasting "+(System.currentTimeMillis()-starttime)+ "ms, a="+sy.a+", " +(sy.a/(float)(cyclenumber*threadsnumber))+" remained-------");
// for(int i=0;i
// {
// threads.remove(0);
// }
// starttime = System.currentTimeMillis();
// atomicTest sy = new atomicTest(cyclenumber);
// for(int i=0;i
// {
// threads.add(new Thread(sy));
// threads.get(i).start();
// }
// for(int i=0;i
// {
// threads.get(i).join();
// }
// System.out.println("method freeTest: lasting "+(System.currentTimeMillis()-starttime)+ "ms, a="+sy.a+", " +(sy.a.get()/(float)(cyclenumber*threadsnumber))+" remained-------");
// for(int i=0;i
// {
// threads.remove(0);
// }
}
}
class freeTest implements Runnable{
public Integer a = 0;
int cycle = 0;
public freeTest(int cyclenumber) {
// TODO Auto-generated constructor stub
cycle = cyclenumber;
}
public void run() {
// TODO Auto-generated method stub
for(int i=0;i
{
a++;
}
}
}
//method freeTest: lasting 10782ms, a=44054841, 0.08810968 remained-------
class synchronizeTest implements Runnable{
public Integer a = 0;
int cycle = 0;
static Object mutex = new Object();
public synchronizeTest(int cyclenumber) {
// TODO Auto-generated constructor stub
cycle = cyclenumber;
}
public void run() {
// TODO Auto-generated method stub
synchronized(mutex)
{
for(int i=0;i
{
a++;
}
}
}
}
//method freeTest: lasting 1814ms, a=500000000, 1.0 remained-------
class lockTest implements Runnable{
public Integer a = 0;
int cycle = 0;
ReentrantLock lock = new ReentrantLock();
public lockTest(int cyclenumber) {
// TODO Auto-generated constructor stub
cycle = cyclenumber;
}
public void run() {
// TODO Auto-generated method stub
for(int i=0;i
{
lock.lock();
a++;
lock.unlock();
}
}
}
//method freeTest: lasting 13897ms, a=500000000, 1.0 remained-------
class atomicTest implements Runnable{
public AtomicInteger a = new AtomicInteger(0);
int cycle = 0;
public atomicTest(int cyclenumber) {
// TODO Auto-generated constructor stub
cycle = cyclenumber;
}
public void run() {
// TODO Auto-generated method stub
for(int i=0;i
{
a.incrementAndGet();
}
}
}
//method freeTest: lasting 13289ms, a=500000000, 1.0 remained-------