最近工作中遇到了個問題,需要使用非同步呼叫某方法(大致意思就是新開乙個執行緒去執行該任務)
直接使用了springboot中的 async
目錄結構:
配置類:
非同步類package com.study.asynctask.asynchron;
import org.apache.tomcat.util.threads.threadpoolexecutor;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import org.springframework.core.task.taskexecutor;
import org.springframework.scheduling.annotation.enableasync;
import org.springframework.scheduling.concurrent.threadpooltaskexecutor;
@configuration
@enableasync
public class asynctaskpool
}
測試類:package com.study.asynctask.asynchron;
import org.slf4j.logger;
import org.slf4j.logge***ctory;
import org.springframework.scheduling.annotation.async;
import org.springframework.stereotype.service;
@service
public class asynctask
}
啟動服務,訪問test路徑 http://localhost:8080/testpackage com.study.asynctask.controller;
import com.study.asynctask.asynchron.asynctask;
import org.slf4j.logger;
import org.slf4j.logge***ctory;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.web.bind.annotation.restcontroller;
@restcontroller
public class asynctestcontroller
}
列印日誌為:
非同步 非同步任務
非同步任務 記錄一下學習完springboot後進行使用非同步方法時的筆記 service層 author yesijie date 2019 11 29 15 25 service public class asyncc catch interruptedexception e system.ou...
非同步任務 定時任務
一 在主程式開啟非同步註解功能 enableasync enableasync 開啟非同步註解功能 public class 二 標明非同步類 asyns service async 表注該類為非同步類 public class asynservice catch interruptedexcept...
AsyncTask非同步任務
asynctask非同步任務怎麼寫 寫乙個類繼承asynctask,重寫方法 doinbackgroud 再new 這個類,執行物件 execute 在ui執行緒裡面寫這個類的實現方法 doinbackground有可能是新開的執行緒,有可能是執行緒池裡面的執行緒 執行順序 onpreexecute...