1、有问题的写法
AdminController文件
package com.controller;
import com.myThread.AdminThread;
import com.myThread.MyCallable;
import com.myThread.MyRunnable;
import org.springframework.web.bind.annotation.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
@RestController
@CrossOrigin
@RequestMapping("/admin")
public class AdminController{
public static int count = 10;
@GetMapping("/{id}")
public long findById(@PathVariable Long id) throws InterruptedException {
AdminThread t1 = new AdminThread();
AdminThread t2 = new AdminThread();
t1.start();
t2.start();
return id;
}
}
AdminThread文件
package com.myThread;
import com.controller.AdminController;
public class AdminThread extends Thread{
@Override
public void run() {
while (true){
if(AdminController.count == 0){
break;
}
AdminController.count--;
System.out.print(getName()+"线程"+AdminController.count+"\n");
}
}
}
执行结果,可以看到重复执行,不安全
解决方法加锁
AdminController文件
package com.controller;
import com.myThread.AdminThread;
import com.myThread.MyCallable;
import com.myThread.MyRunnable;
import org.springframework.web.bind.annotation.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
@RestController
@CrossOrigin
@RequestMapping("/admin")
public class AdminController{
//数量
public static int count = 10;
//锁对象
public static final Object lock = new Object();
public static boolean flag = false;
@GetMapping("/{id}")
public long findById(@PathVariable Long id) throws InterruptedException {
AdminThread t1 = new AdminThread();
AdminThread t2 = new AdminThread();
t1.start();
t2.start();
return id;
}
}
AdminThread文件
package com.myThread;
import com.controller.AdminController;
public class AdminThread extends Thread{
@Override
public void run() {
while (true){
synchronized(AdminController.lock){
if(AdminController.count == 0){
break;
}else{
if(!AdminController.flag){
//操作
AdminController.count--;
System.out.print(getName()+"线程"+AdminController.count+"\n");
//释放
AdminController.lock.notifyAll();
}else{
//一直循环监听
try {
AdminController.lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
}
AdminThread上面这个!会有空执行(虽然没有错,但多跑了一点点),应该下面这样
package com.myThread;
import com.controller.AdminController;
public class AdminThread extends Thread{
@Override
public void run() {
while (true){
synchronized(AdminController.lock){
if(AdminController.count == 0){
break;
}else{
//操作
AdminController.count--;
System.out.print(getName()+"线程"+AdminController.count+"\n");
//释放
AdminController.lock.notifyAll();
}
}
}
}
}