0041 Java學習筆記-多線程-線程池、ForkJoinPool、ThreadLocal。本站提示廣大學習愛好者:(0041 Java學習筆記-多線程-線程池、ForkJoinPool、ThreadLocal)文章只能為提供參考,不一定能成為您想要的結果。以下是0041 Java學習筆記-多線程-線程池、ForkJoinPool、ThreadLocal正文
開端時間
開端計算完畢時間
點示例一:
package testpack;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Test2 {
public static void main(String[] args) throws InterruptedException{
Runnable task=()->{ //創立一個線程義務
for (int i=0;i<1;i++) {
System.out.println(new Date());
try{
Thread.sleep(3000);
}catch(InterruptedException ex){
ex.printStackTrace();
}
System.out.println(new Date());
}
};
ScheduledExecutorService ses=Executors.newScheduledThreadPool(1); //創立一個單線程延遲處置處置線程池,
ses.scheduleAtFixedRate(task,1,5,TimeUnit.SECONDS); //標志㈠。延遲1秒開端處置,從開端處置的時間點開端算,5秒後執行第二個周期
}
}
輸入:
Fri Dec 09 16:22:07 CST 2016 //07秒開端處置
Fri Dec 09 16:22:10 CST 2016 //run中暫停3秒
Fri Dec 09 16:22:12 CST 2016 //07+5秒開端第二個周期
Fri Dec 09 16:22:15 CST 2016 //run中暫停3秒
Fri Dec 09 16:22:17 CST 2016 //07+5+5開端第三個周期
示例二:將下面標志㈠的辦法改為:scheduleWithFixedDelay,輸入如下:
Fri Dec 09 16:28:35 CST 2016 //35秒開端處置
Fri Dec 09 16:28:38 CST 2016 //run中暫停3秒
Fri Dec 09 16:28:43 CST 2016 //38+5秒開端第二個周期
Fri Dec 09 16:28:46 CST 2016 //run中暫停3秒
Fri Dec 09 16:28:51 CST 2016 //46+5秒開端第三個周期
package testpack;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;
import java.util.concurrent.TimeUnit;
public class Test2 {
public static void main(String[] args) throws InterruptedException{
ForkJoinPool pool=new ForkJoinPool();
pool.submit(new Task(0,327));
pool.awaitTermination(2,TimeUnit.SECONDS);
pool.shutdown();
}
}
class Task extends RecursiveAction{
private static final int THRESHOLD=50;
private int start;
private int end;
public Task(int start,int end){
this.start=start;
this.end=end;
}
protected void compute(){
if (end-start<THRESHOLD) {
for (int i=start;i<end;i++){
System.out.println(Thread.currentThread().getName()+"輸入: "+i);
}
}else{
int middle = (start+end)/2;
Task left=new Task(start,middle);
Task right=new Task(middle,end);
left.fork();
right.fork();
}
}
}
package testpack;
public class Test1 {
public static void main(String[] args){
Account a=new Account("初始名字");
new MyThread(a).start();
new MyThread(a).start();
}
}
class MyThread extends Thread{
private Account account;
public MyThread(Account account){
this.account=account;
}
public void run(){
for (int i=0;i<5;i++) {
if (i==3){
account.setName(Thread.currentThread().getName());
}
System.out.println("線程稱號:"+getName()+" ,賬戶名:"+account.getName()+" 輸入:"+i);
}
}
}
class Account {
private ThreadLocal<String> name=new ThreadLocal<>();
public Account(String str){
this.name.set(str);
System.out.println("結構時的賬戶名:"+this.name.get());
}
public String getName(){
return name.get();
}
public void setName(String str){
this.name.set(str);
}
}
輸入:
結構時的賬戶名:初始名字
線程稱號:Thread-0 ,賬戶名:null 輸入:0
線程稱號:Thread-1 ,賬戶名:null 輸入:0
線程稱號:Thread-0 ,賬戶名:null 輸入:1
線程稱號:Thread-1 ,賬戶名:null 輸入:1
線程稱號:Thread-0 ,賬戶名:null 輸入:2
線程稱號:Thread-1 ,賬戶名:null 輸入:2 //初始值為null
線程稱號:Thread-0 ,賬戶名:Thread-0 輸入:3
線程稱號:Thread-0 ,賬戶名:Thread-0 輸入:4
線程稱號:Thread-1 ,賬戶名:Thread-1 輸入:3
線程稱號:Thread-1 ,賬戶名:Thread-1 輸入:4