6.3 多线程的安全与应用
2023-12-27 23:31:26
6.3 多线程的安全与应用
1. 多线程概念
1. 进程
2. 线程
2. 创建多线程
1. 继承Thread类创建线程
package com.imooc.thread;
import java.util.Random;
//继承Thread实现多线程程序
public class ThreadSample1 {
/**
* 创建跑步线程
* 继承Thread,重写run方法实现业务需求
*/
class Runner extends Thread {
@Override
public void run() {
Integer speed = new Random().nextInt(10);
for (int i = 1; i <= 10; i++) {
try {
Thread.sleep(1000);//使线程休眠,单位:毫秒
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("第" + i + "秒:" + this.getName() + "已跑到" + (i * speed) + "米(" + speed + "米/秒)");
}
}
}
/**
* 创建线程并开启
*
* @param ThreadName 自定义线程名
*/
public void start(String ThreadName) {
Runner thread = new Runner();//创建跑步线程
thread.setName(ThreadName);//设置线程名
thread.start();//开启线程
}
public static void main(String[] args) {
new ThreadSample1().start("选手1");
new ThreadSample1().start("选手2");
new ThreadSample1().start("选手3");
new ThreadSample1().start("选手4");
}
}
2. 实现Runnable接口创建线程
package com.imooc.thread;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
//实现Runnable接口实现多线程程序
public class ThreadSample2 {
//线程池,容量:20
private ExecutorService frService = Executors.newFixedThreadPool(20);
class Runner implements Runnable{
//跑步名称
private String runName;
public Runner(String runName) {
this.runName = runName;
}
@Override
public void run() {
Integer speed = new Random().nextInt(10);
for(int i = 1 ; i <= 10 ; i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("第" + i + "秒:" + runName + "已跑到" + (i * speed) + "米(" + speed + "米/秒)");
}
}
}
/**
* 创建线程并开启
* @param runerName
*/
public void start(String runerName){
Runner runner = new Runner(runerName);//创建线程规则
Thread thread = new Thread(runner);//创建线程
thread.start();//开启线程
}
/**
* 平替,代码较少
* @param runerName
*/
public void startExecutors(String runerName){
frService.submit(new Runner(runerName));//创建并开启线程
}
public static void main(String[] args) {
new ThreadSample2().start("选手A");
new ThreadSample2().start("选手B");
new ThreadSample2().start("选手C");
new ThreadSample2().startExecutors("选手1");
new ThreadSample2().startExecutors("选手2");
new ThreadSample2().startExecutors("选手3");
}
}
3. Callable接口创建线程
package com.imooc.thread;
import java.util.Random;
import java.util.concurrent.*;
//实现Callable接口实现多线程程序
public class ThreadSample3 {
class Runner implements Callable<Integer>{
public String name;
@Override
public Integer call() throws Exception {
Integer speed = new Random().nextInt(10);
Integer result = 0;
for(int i = 1 ; i <= 10 ; i++){
Thread.sleep(1000);
result = i * speed;
System.out.println("第" + i + "秒:" + this.name + "已跑到" + (i * speed) + "米(" + speed + "米/秒)");
}
return result;
}
}
public void start() throws ExecutionException, InterruptedException {
//创建线程池
ExecutorService executorService = Executors.newFixedThreadPool(3);
Runner threadA = new Runner();
threadA.name = "参赛者A";
Runner threadB = new Runner();
threadB.name = "参赛者B";
Runner threadC = new Runner();
threadC.name = "参赛者C";
//利用Future对象获取每一个线程执行后的结果
Future<Integer> r1 = executorService.submit(threadA);
Future<Integer> r2 = executorService.submit(threadB);
Future<Integer> r3 = executorService.submit(threadC);
//关闭线程池
executorService.shutdown();
System.out.println(threadA.name + "累计跑了" + r1.get() + "米");
System.out.println(threadB.name + "累计跑了" + r2.get() + "米");
System.out.println(threadC.name + "累计跑了" + r3.get() + "米");
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
new ThreadSample3().start();
}
}
3. 线程同步
4. 线程安全
5. 线程池应用
文章来源:https://blog.csdn.net/qq_44289784/article/details/135213893
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!