一、创建多线程的三种方式
1.1继承Thread
继承Thread,重新run方法,调用start()方法
public class Thread1 extends Thread {
@Override
public void run() {
System.out.println("000000");
}
public static void main(String[] args) {
Thread1 testThread = new Thread1();
testThread.start();
}
}
1.2实现runnable
实现runnable,实现run()方法,用Threa的start()方法静态代理执行
public class CreateRunnable implements Runnable {
@Override
public void run() {
System.out.println("000000");
}
public static void main(String[] args) {
CreateRunnable testThread = new CreateRunnable();
Thread thread = new Thread(testThread);
thread.start();
}
}
1.3实现callable(有返回值)
线程池执行,获取返回值
public class MyCallable implements Callable<String> {
@Override
public String call() {
System.out.println(Thread.currentThread().getName() + " 执行callable的call方法");
return "result";
}
public static void main(String[] args) {
test2();
}
public static void test2() {
int threadNum = 5;
ExecutorService es = Executors.newFixedThreadPool(threadNum);
List<Future<String>> futures = new ArrayList<Future<String>>(threadNum);
for (int i = 0; i < threadNum; i++) {
Future<String> future = es.submit(new MyCallable());
futures.add(future);
}
es.shutdown();
for (Future<String> future : futures) {
try {
String result = future.get();
System.out.println(Thread.currentThread().getName() + "\t" + result);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
}
二、线程同步
2.1synchronized 关键字
1、synchronized 同步方法,锁的是this
2、同步方法块synchronized (obj){},需要放入同步的对象,当作锁
public class Demo27_SafeBuyTicket {
public static void main(String[] args) {
BuyTicket1 buyTicket = new BuyTicket1();
new Thread(buyTicket, "张三").start();
new Thread(buyTicket, "李四").start();
new Thread(buyTicket, "王五").start();
}
}
class BuyTicket1 implements Runnable {
private int ticketNums = 10;
boolean flag = true;
@Override
public void run() {
while (flag) {
try {
buy();
} catch (Exception e) {
e.printStackTrace();
}
}
}
private synchronized void buy() {
if (ticketNums <= 0) {
flag = false;
return;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "拿到" + ticketNums--);
}
}
2.2lock锁
显式的加锁和解锁,常用的锁:ReentrantLock 重入锁
public class Demo32_ThreadLock {
public static void main(String[] args) {
TestLock testLock = new TestLock();
new Thread(testLock).start();
new Thread(testLock).start();
new Thread(testLock).start();
}
}
class TestLock implements Runnable {
int tickerNums = 10;
private final ReentrantLock lock = new ReentrantLock();
@Override
public void run() {
while (true) {
try {
lock.lock();
if (tickerNums <= 0) {
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(tickerNums--);
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
}
三、扩展:使用 Spring的@Async解决IO操作
注意事项:如下方式会使@Async失效
异步方法使用static修饰
异步类没有使用@Component注解(或其他注解)导致spring无法扫描到异步类
异步方法不能与被调用的异步方法在同一个类中
类中需要使用@Autowired或@Resource等注解自动注入,不能自己手动new对象
如果使用SpringBoot框架必须在启动类中增加@EnableAsync注解
3.1配置类
@Configuration
@EnableAsync
public class ThreadPoolTaskConfig {
private static final int corePoolSize = 20;
private static final int maxPoolSize = 100;
private static final int keepAliveTime = 10;
private static final int queueCapacity = 200;
private static final String threadNamePrefix = "Async-Service-";
@Bean("taskExecutor")
public ThreadPoolTaskExecutor taskExecutor(){
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(corePoolSize);
executor.setMaxPoolSize(maxPoolSize);
executor.setQueueCapacity(queueCapacity);
executor.setKeepAliveSeconds(keepAliveTime);
executor.setThreadNamePrefix(threadNamePrefix);
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.initialize();
return executor;
}
}
3.2接口
@GetMapping("/sss")
public JSONArray getIndustryOverview(String areacode) {
service.sendMessage1();
Future userNum3 = service.findUserNum2(areacode);
try {
System.err.println("获取结果");
Object o2 = userNum3.get();
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
3.3service
@Service
public class TranTest2Service {
Logger log = LoggerFactory.getLogger(TranTest2Service.class);
@PostConstruct
@Async("taskExecutor")
public void sendMessage1() throws InterruptedException {
log.info("发送短信方法---- 1 执行开始");
Thread.sleep(5000);
log.info("发送短信方法---- 1 执行结束");
}
@Async("taskExecutor")
public Future findUserNum2(String areacode) {
AsyncResult<Integer> asyncResult = null;
try {
EpointFrameDsManager.begin();
sql = "select sum(cyrys) num from trq_userinfo where areacode=?";
asyncResult = new AsyncResult<>(this.queryInt(sql, areacode));
EpointFrameDsManager.commit();
}
catch (Exception e) {
EpointFrameDsManager.rollback();
}
finally {
EpointFrameDsManager.close();
}
return asyncResult;
}
}