admin 管理员组

文章数量: 1086019


2024年3月30日发(作者:haproxy 负载均衡)

优秀的java代码案例

/**。

* 使用 FutureTask 类来演示 Callable 和 Future 接口的用法。

*/。

public class FutureTaskExample 。

public static void main(String[] args) throws

InterruptedException, ExecutionException 。

MyCallable callable1 = new MyCallable(1000);。

MyCallable callable2 = new MyCallable(2000);。

FutureTask futureTask1 = new

FutureTask(callable1);。

FutureTask futureTask2 = new

FutureTask(callable2);。

new Thread(futureTask1).start();。

new Thread(futureTask2).start();。

while (true) 。

if (() && ()) 。

n("Done");。

//shut down executor service。

break;。

}。

if (!()) 。

n("FutureTask1 output=" +

());。

}。

String s = (200L, ECONDS);。

if (s != null) 。

n("FutureTask2 output=" + s);。

}。

}。

}。

}。

class MyCallable implements Callable

private long waitTime;。

public MyCallable(int timeInMillis)。

me=timeInMillis;。

}。

public String call() throws Exception 。

(waitTime);。

//return the thread name executing this callable task。

return tThread().getName();。

}。


本文标签: 类来 负载 案例 用法 作者