一、手写分布式事务
package com.itheima.security.springboot.juc.transaction.onsistency;
import java.util.ArrayList;
import java.util.List;
/**
* @Title: 分布式事务:模拟ABC三个任务,任何一个任务失败则三个任务回滚
* @Description: TODO
* @throws
* @Author: Vector
* @DateTime:
*/
public class T00_F6 {
public static void main(String[] args) throws Exception {
Boss boss = new Boss();
Worker t1 = new Worker(boss, "t1", 3, true);
Worker t2 = new Worker(boss, "t2", 4, true);
Worker t3 = new Worker(boss, "t3", 1, false);
boss.addTask(t1);
boss.addTask(t2);
boss.addTask(t3);
boss.start();
System.in.read();
}
private static enum Result {
NOTSET, SUCCESS, FAILED, CANCELLED
}
private static class Boss extends Thread {
private List<Worker> workers = new ArrayList<>();
public void addTask(Worker worker) {
workers.add(worker);
}
@Override
public void run() {
workers.forEach(work -> work.start());
}
public void end(Worker worker) {
if (worker.getResult() == Result.FAILED) {
calcel(worker);
}
}
private void calcel(Worker worker) {
for (Worker w : workers) {
if (w != worker) {
w.cancel();
}
}
}
}
private static class Worker extends Thread {
private Result result = Result.NOTSET;
private Boss boss;
private String name;
private int timeInSeconds;
private boolean success;
private volatile boolean cancelling = false;
public Worker(Boss boss, String name, int timeInSeconds, boolean success) {
this.boss = boss;
this.name = name;
this.timeInSeconds = timeInSeconds;
this.success = success;
}
public Result getResult() {
return result;
}
@Override
public void run() {
int interval = 100;
int total = 0;
//启动监视,有失败的任务,系统停止
for (; ; ) {
try {
Thread.sleep(interval); //每隔100毫秒去看任务任务是否被取消
} catch (InterruptedException e) {
e.printStackTrace();
}
total += interval;
if (total / 1000 >= timeInSeconds) {
System.out.println(name + "任务结束!" + result); //正常结束
result = success ? Result.SUCCESS : Result.FAILED;
break;
}
if (cancelling) {
rollback();
result = Result.CANCELLED;
cancelling = false;
System.out.println(name + "任务结束!" + result);
break;
}
}
//模拟任务执行时间
//实际中时间不固定,可能在处理计算任务或者是IO任务
boss.end(this);
}
private void rollback() {
//如何书写回滚
System.out.println(name + "rollback start");
//不同的业务有不同的回滚犯法
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(name + "rollback end");
}
public void cancel() {
//思考以下,如何才能calcel
cancelling = true;
// 思考一下,在run中如何处理
}
}
}
二、CF
package com.itheima.security.springboot.juc.transaction.onsistency;
import java.util.concurrent.CompletableFuture;
public class T06_AliQuestionAboutCF {
public static void main(String[] args) throws Exception {
MyTask task1 = new MyTask("t1", 3, true);
MyTask task2 = new MyTask("t2", 4, true);
MyTask task3 = new MyTask("t3", 1, false);
CompletableFuture<Void> f1 = CompletableFuture.supplyAsync(() -> task1.call())
.thenAccept((result) -> callback(result));
CompletableFuture<Void> f2 = CompletableFuture.supplyAsync(() -> task2.call())
.thenAccept((result -> callback(result)));
CompletableFuture<Void> f3 = CompletableFuture.supplyAsync(() -> task3.call())
.thenAccept((result -> callback(result)));
System.in.read();
}
private static void callback(boolean result) {
if (false == result) {
//处理结束流程
//通知其他线程结束(回滚)
//超时处理
System.exit(0); //太过暴力
}
}
private static class MyTask {
private String name;
private int timeInSeconds;
private boolean success;
public MyTask(String name, int timeInSeconds, boolean success) {
this.name = name;
this.timeInSeconds = timeInSeconds;
this.success = success;
}
public Boolean call() {
//模拟业务执行时间
try {
Thread.sleep(timeInSeconds);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(name + "任务执行结束");
return true;
}
}
}
一、手写分布式事务
一、手写分布式事务