zoukankan      html  css  js  c++  java
  • 事务回滚

    package com.test;
    
    import java.util.concurrent.CountDownLatch;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.atomic.AtomicBoolean;
    
    public class Test {
    
    	private static ExecutorService exec = Executors.newCachedThreadPool();
    
    	public static void main(String[] args) throws InterruptedException, ExecutionException {
    		CountDownLatch mainThreadCount = new CountDownLatch(10);
    		CountDownLatch rollBackCount = new CountDownLatch(1);
    		AtomicBoolean rollbackFlag = new AtomicBoolean(false);
    		for (int i = 0; i < 10; i++) {
    			exec.execute(new TaskWithoutResult(i, mainThreadCount, rollBackCount, rollbackFlag));
    		}
    
    		// 主线程业务执行完毕 如果其他线程也执行完毕 且没有报异常 正在阻塞状态中 唤醒其他线程 提交所有的事务
    		// 如果其他线程或者主线程报错 则不会进入if 会触发回滚
    		if (!rollbackFlag.get()) {
    			mainThreadCount.await();
    			rollBackCount.countDown();
    		}
    	}
    }
    
    package com.test;
    
    import java.util.concurrent.CountDownLatch;
    import java.util.concurrent.atomic.AtomicBoolean;
    
    class TaskWithoutResult implements Runnable {
    
    	private int id;
    	private CountDownLatch mainThreadCount;
    	private CountDownLatch rollBackCount;
    	private AtomicBoolean rollbackFlag;
    
    	public TaskWithoutResult(int id, CountDownLatch cout, CountDownLatch rollBackCount, AtomicBoolean rollbackFlag) {
    		this.id = id;
    		this.mainThreadCount = cout;
    		this.rollBackCount = rollBackCount;
    		this.rollbackFlag = rollbackFlag;
    	}
    
    	@Override
    	public void run() {
    		if (rollbackFlag.get()) {
    			return;
    		}
    		try {
    			Thread.sleep(1000 * id);
    			if (id == 3) {
    				//int a = id / 0;
    			}
    			mainThreadCount.countDown();
    			rollBackCount.await();// 我执行完了,我等着看别人执行结果是不是有回滚
    			if (rollbackFlag.get()) {
    				System.out.println("线程" + id + "回滚事务");
    				// 回滚
    			} else {
    				// 提交我的事务
    				System.out.println("线程" + id + "提交事务");
    			}
    		} catch (Exception ex) {
    			// 如果出错了 就放开锁 让别的线程进入提交/回滚 本线程进行回滚
    			rollbackFlag.set(true);
    			rollBackCount.countDown();
    			mainThreadCount.countDown();
    			// 自己出错了要回滚自己
    			///////////
    			System.out.println("线程" + id + "回滚事务");
    		}
    	}
    }
    

      

  • 相关阅读:
    eclipse中,把java函数代码折叠/展开 介绍【转】
    苹果开发者账号注册&真机调试
    Objective-C编码规范:26个方面解决iOS开发问题
    iTunes获取下载的安装包
    Mac AppStore下载文件的获取
    Mac 切换Windows 使用虚拟机, 不推荐双系统
    Xcode使用版本
    如何提高代码质量
    ARC的内存管理
    Objective-C 类的继承、方法的重写和重载
  • 原文地址:https://www.cnblogs.com/a5513633/p/13949603.html
Copyright © 2011-2022 走看看