import java.util.concurrent.*; public class Demo{ public static void main(String [] args){ ExecutorService executor=Executors.newCachedThreadPool(); Task task=new Task(); Future<Integer> result=executor.submit(task); executor.shutdown(); System.out.println("main thread is running"); try{ System.out.println("task result="+result.get()); }catch(Exception e){ e.printStackTrace(); } System.out.println("over"); } } class Task implements Callable<Integer>{ public Integer call() throws Exception{ System.out.println("sub thread is running"); Thread.sleep(3000); int sum = 0; for(int i=0;i<101;i++) sum += i; return sum; } }
import java.util.concurrent.*; public class Demo{ public static void main(String [] args){ ExecutorService executor=Executors.newCachedThreadPool(); FutureTask<Integer> futureTask=new FutureTask<Integer>(new Task()); executor.submit(futureTask); executor.shutdown(); System.out.println("main thread is running"); try{ System.out.println("task result="+futureTask.get()); }catch(Exception e){ e.printStackTrace(); } System.out.println("over"); } } class Task implements Callable<Integer>{ public Integer call() throws Exception{ System.out.println("sub thread is running"); Thread.sleep(3000); int sum = 0; for(int i=0;i<101;i++) sum += i; return sum; } }
import java.util.concurrent.*; class Util{ public static long testTime(int nThreads,final Runnable task) throws InterruptedException { //为启动门设置一个门闩,当门闩被打开时,放行所有被挡在门外的线程 final CountDownLatch startGate=new CountDownLatch(1); //为结束门设置n个门闩,当n个门闩被打开时,放行所有被挡在门外的线程 final CountDownLatch endGate=new CountDownLatch(nThreads); //测试n个线程并发执行任务task的时间 for(int i=0;i<nThreads;i++){ Thread t=new Thread(){ public void run(){ try { startGate.await(); try{ task.run(); }catch(Exception e){ endGate.countDown(); } } catch (InterruptedException e) {} } }; t.start(); } //循环中的内容使得有n个线程在startGate门外等着执行task任务 long start=System.nanoTime(); startGate.countDown();//打开startGate上的门闩 endGate.await();//等待endGate门开 long end =System.nanoTime(); return end-start; } } pub
import java.util.concurrent.*; public class Demo{ public static void main(String [] args) throws InterruptedException { final CyclicBarrier cyclicBarrier=new CyclicBarrier(10, new Runnable() { @Override public void run() { // TODO Auto-generated method stub System.out.println("10 个人都到达会议室,开会"); } }); for(int i=0;i<10;i++){ final long tmp=i; Thread thread=new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(1000*(11-tmp)); System.out.println("person"+tmp+" come here"); try { cyclicBarrier.await();//等待其他线程到达 } catch (BrokenBarrierException e) { e.printStackTrace(); } } catch (InterruptedException e) {} } }); thread.start(); } } }
class MyHashSet<T>{ private Set<T> set; private Semaphore semaphore; public MyHashSet(int sem){ this.semaphore=new Semaphore(sem); this.set=Collections.synchronizedSet(new HashSet<T>()); } public boolean add(T o) throws InterruptedException{ semaphore.acquire(); boolean isAdded=false; try{ isAdded=this.set.add(o); return isAdded; }finally{ if(!isAdded){ semaphore.release(); } } } public boolean remove(T o){ boolean isRemoved=this.set.remove(o); if(isRemoved) semaphore.release(); return isRemoved; } }
藏家571 2025-03-21
二三 2025-03-21
藏家241 2025-03-21
藏家380 2025-03-21
玄藏 2025-03-21
藏家613 2025-03-21
藏家976 2025-03-21
藏家160 2025-03-21
藏家588 2025-03-21
瑄 2025-03-21