concurrent包学习demo1--备忘

luozhangwen 2010-07-26 04:41:14

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);
}

}
}

...全文
102 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
luozhangwen 2010-08-28
  • 打赏
  • 举报
回复
自认为比较好的线程超时控制.


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);
}
}

luozhangwen 2010-07-27
  • 打赏
  • 举报
回复
to: 火龙果, 因为发blog这里有数据量, blog里面东西太多, 发不了几个字, 但是发帖子就不一样

to: LV9,
发新帖东西容易归类, 容易找一点,收藏到网摘后我可以通过标题去找类似东西. 东西都发一个贴里面不好找东西.
BearKin 2010-07-27
  • 打赏
  • 举报
回复
...其实不用新发个贴 随便找一个以前的贴一发就好了被。。我通常用这种方式获得CSDN论坛的代码样式
  • 打赏
  • 举报
回复
为什么你特别喜欢把 CSDN 论坛当作博客?
luozhangwen 2010-07-26
  • 打赏
  • 举报
回复
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请求需要大量时间执行.";
}
}
luozhangwen 2010-07-26
  • 打赏
  • 举报
回复

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();
}

}

23,404

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧