23,404
社区成员
发帖
与我相关
我的任务
分享
package com.lzw;
import java.util.Random;
import java.util.concurrent.*;
import java.util.concurrent.locks.*;
public class MySemaphore
{
private static final int MAX_AVAILABLE = 3;
public static void main(String[] args)
{
ExecutorService service = Executors.newCachedThreadPool();
final Semaphore sp = new Semaphore(MAX_AVAILABLE); //线程计数器
final Lock lock = new ReentrantLock();
for (int i = 0; i < 10; i++)
{
Runnable runnable = new Runnable()
{
public void run()
{
try
{
sp.acquire();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
String threadName = Thread.currentThread().getName();
//最大可用 - 可用的 = 当前已经使用的
int usedThread = MAX_AVAILABLE - sp.availablePermits();
System.out.println("线程" + threadName + "进入,当前已有"
+ usedThread + "个并发");
try
{
//System.out.println(Math.round(Math.random()*10000));
Thread.sleep(new Random().nextInt(10000));
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println("线程" + threadName + "即将离开");
sp.release();
System.out.println("线程" + threadName
+ "已离开,当前已有"
+ (3 - sp.availablePermits()) + "个并发" );
}
};
service.execute(runnable);
}
}
}
package com.lzw;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
class CopyOfCounterThread extends Thread
{
public static boolean executeTimeControlMethod(Runnable runable,
long timeout)
{
// 创建一个使用单个 worker 线程的 Executor
ExecutorService service = Executors.newSingleThreadExecutor();
service.execute(runable);
boolean flag = false;
try
{
service.shutdown();
flag = service.awaitTermination(timeout, TimeUnit.MILLISECONDS);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
return flag;
}
public static void main(String[] args)
{
boolean flag = CopyOfCounterThread.executeTimeControlMethod(new Thread()
{
public void run()
{
try
{
Thread.sleep(1000);
System.out.println("线程关闭了.");
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}, 500);
System.out.println(flag);
}
}
package com.lzw;
import java.util.concurrent.*;
public class CallableAndFuture
{
public static void main(String[] args) throws Exception
{
ExecutorService service = Executors.newCachedThreadPool();
Future<String> future = service.submit(new MyCallable());
CompletionService<Integer> completionService = new ExecutorCompletionService<Integer>(service);
System.out.println("异步IO已经被跳过, 继续往下执行.");
for (int i = 0; i < 5; i++)
{
final Integer seq = i + 1;
completionService.submit(new Callable<Integer>()
{
public Integer call() throws Exception
{
return seq;
}
});
}
for (int i = 0; i < 5; i++)
{
Future<Integer> future2 = completionService.take();
Thread.sleep(1000);
System.out.println(future2.get());
}
System.out.println(future.get());
}
}
class MyCallable implements Callable<String>
{
public String call() throws Exception
{
System.out.println("开始读取IO, 需要时间8000MS");
Thread.sleep(8000);
return "读取异步IO完成,一般用于IO请求需要大量时间执行.";
}
}
package com.lzw;
import java.util.Random;
import java.util.concurrent.*;
import java.util.concurrent.locks.*;
public class MyLockTest
{
public static void main(String[] args)
{
ExecutorService service = Executors.newCachedThreadPool();
final Business1 business1 = new Business1();
final Business2 business2 = new Business2();
for (int i = 0; i < 10; i++)
{
// service.execute(new Runnable()
// {
// public void run()
// {
// business1.service();
// }
// });
service.execute(new Runnable(){
public void run()
{
business2.write(new Random().nextInt(1000));
}
});
service.execute(new Runnable(){
public void run()
{
business2.read();
}
});
}
service.shutdown();
}
}
/**
* 线程锁
*/
class Business1
{
private int count = 0;
Lock lock = new ReentrantLock();
public void service()
{
lock.lock();
count++;
try
{
Thread.sleep(new Random().nextInt(100));
}
catch (InterruptedException e)
{
e.printStackTrace();
}
finally
{
System.out.println("count=" + count);
count = 0;
lock.unlock();
}
}
}
/**
* 读写锁.
*/
class Business2
{
ReadWriteLock lock = new ReentrantReadWriteLock();
private Integer date = null;
public void read()
{
lock.readLock().lock();
System.out.println("readLock!");
try
{
Thread.sleep(new Random().nextInt(100));
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println("date = " + this.date);
System.out.println("unReadLocak!");
System.out.println();
lock.readLock().unlock();
}
public void write(Integer date)
{
lock.writeLock().lock();
System.out.println("writeLock!");
try
{
Thread.sleep(new Random().nextInt(100));
}
catch (InterruptedException e)
{
e.printStackTrace();
}
this.date = date;
System.out.println("unWriteLock!");
lock.writeLock().unlock();
}
}