Java??????????????
?????Linux??? ???????[ 2017/4/25 11:00:26 ] ??????????????????? JAVA
????java ???????????????
??????java?У???????????????????????????????????Thread????дrun???????????????????Runnable???????дrun????????????????????Callable??????дcall???????????????????????????????????????????????????ο?????
????(1)???Thread????дrun??????
class xx extends Thread{
public void run(){
Thread.sleep(1000) //???????1000????sleep???????Block????????????
}}
????????????
????????.start() //???????run????????
??????2?????Runnable????????????
class MyThread implements Runnable {
private String name;
public MyThread(String name) {
super();
this.name = name;
}
@Override
public void run() {
for(int i = 0 ; i < 200; i++) {
System.out.println("Thread"+name+"--->"+i);
}
}
}
public class ThreadDemo {
public static void main(String[] args) {
MyThread a = new MyThread("a");
MyThread b = new MyThread("b");
MyThread c = new MyThread("c");
new Thread(a).start();
new Thread(b).start();
new Thread(c).start();
}
}
????(3)???Callable??????дcall????
????Callable????????Runnable????????Callable??????????Runnable?????????????????е?????
????Callable??Runnable?м????:
????Callable?漲???????call()????Runnable?漲???????run().
????Callable????????к??????????Runnable?????????????????
????call()???????????????run()?????????????????
????????Callable???????????Future????Future???????????????????????????????????????????????????????????????.???Future????????????????????????????????У?????????????е???
????Java Callable ?????????
class TaskWithResult implements Callable<String> {
private int id;
public TaskWithResult(int id) {
this.id = id;
}
@Override
public String call() throws Exception {
return "result of TaskWithResult " + id;
}
}
public class CallableTest {
public static void main(String[] args) throws InterruptedException??
ExecutionException {
ExecutorService exec = Executors.newCachedThreadPool();
ArrayList<Future<String>> results = new ArrayList<Future<String>>(); //Future ?????????????Executor??е????????????
for (int i = 0; i < 10; i++) {
results.add(exec.submit(new TaskWithResult(i)));
}
for (Future<String> fs : results) {
if (fs.isDone()) {
System.out.println(fs.get());
} else {
System.out.println("Future result is not yet complete");
}
}
exec.shutdown();
}
}
??????л???????????????????лл????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11