Home >Java >javaTutorial >Detailed explanation of Java examples: sub-thread task exception, main thread transaction rollback
This article brings you relevant knowledge about java, which mainly introduces the related issues about how to roll back the main thread transaction when an exception occurs in the sub-thread task, including the capture of exceptions. and transaction rollback, etc. Let’s take a look at them below. I hope it will be helpful to everyone.
## Recommended study: "java video tutorial"
The main thread submitted a task to the thread pool. If an exception occurs during the execution of this task, how can the main thread catch the exception and roll back the transaction.
public interface Callable<v> { V call() throws Exception;}</v>
public interface Runnable { public abstract void run();}You can see that the call method has a return value, and the run method has no return value. In addition, the call method can throw exceptions, but the run method cannot. Obviously, in order to capture or know the running results of sub-threads, or running exceptions, we should implement it through the Callable interface. Here we write an ExpSubThread class (sub-thread exception simulation class), implement the Callable interface, and directly throw a null pointer exception without doing too many actions.
public class ExpSubThread implements Callable { @Override public Object call() throws Exception { throw new NullPointerException(); }}3. Thread poolWhen faced with thread tasks, we usually establish a thread pool in advance. The thread pool is a pre-planned collection of n thread resources. Its advantage is:
public ThreadPoolTaskExecutor getThreadPool(){ ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setMaxPoolSize(100); //线程池最大线程数 executor.setCorePoolSize(50);//线程池核心线程数 executor.setQueueCapacity(50);//任务队列的大小 executor.setThreadNamePrefix("test_"); //线程前缀名 executor.initialize(); //线程初始化 return executor;}4. Exception captureThe following is a test case I wrote, where it represents the program execution flow of the main thread
@Testvoid subThreadExceptionTest() { try{ //新建子线程对象 ExpSubThread expSubThread = new ExpSubThread(); //构建线程池 ThreadPoolTaskExecutor executor = getThreadPool(); //提交子线程任务,submit方法 Future future = executor.submit(expSubThread); //在这里可以做主线程的业务其他流程操作 //阻塞等待子线程的执行结果 Object obj = future.get(); }catch (Exception e){ e.printStackTrace(); //事务回滚 }}What needs to be noted here is Use the submit method to submit sub-thread tasks to the thread pool for execution. ThreadPoolTaskExecutor has two methods for executing thread tasks, one is the execute method and the other is the submit method.
V get() throws InterruptedException, ExecutionException;The picture below is the effect of the above test case program
e.printStackTrace();. From the picture you can see two Exception exceptions, one is that we are in the child thread A null pointer exception actively thrown in a simulated manner in the task, and another ExecutionException thrown by the get method caused by a null pointer.
conn.rollback()
realizes transaction rollback @Transactional
annotation in the spring environment. Recommended study: "java video tutorial"
The above is the detailed content of Detailed explanation of Java examples: sub-thread task exception, main thread transaction rollback. For more information, please follow other related articles on the PHP Chinese website!