1,线程,进程,多线程
2,线程创建
Thread,Runnable,Callable
三种创建方式:
Thread
package Thread;
//创建线程方式一:继承Thread类,重写run()方法,调用start开启线程
//总结:
//注意,线程开启不一定立即执行,由cpu调度执行
public class TestThread extends Thread{
@Override
public void run() {
//run方法线程体
for (int i = 0; i < 20; i++) {
System.out.println("我在看代码---"+i);
}
}
public static void main(String[] args) {
//main 线程,主线程
//创建一个线程对象
TestThread testThread = new TestThread();
//调用start()方法开启线程
testThread.start();
for (int i = 0; i <1000; i++) {
System.out.println("我在学习多线程--"+i);
}
}
}
下载三张图片(需要使用commons-io-1.4.jar包)
package Thread;
//联系Thread,实现多线程同步下载图片
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
public class TestThread2 extends Thread{
private String url;//网络图片地址
private String name;//保存的文件名
public TestThread2(String url,String name) {
this.url = url;
this.name = name;
}
@Override
public void run() {
WebDownloader downloader = new WebDownloader();
downloader.downloader(url,name);
System.out.println("下载了文件名为:"+name);
}
public static void main(String[] args) {
TestThread2 t1 = new TestThread2("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fi0.hdslb.com%2Fbfs%2Farticle%2F81dfe3b12316064e56a12ac52e82fab118b8e96e.jpg&refer=http%3A%2F%2Fi0.hdslb.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1614224630&t=8e54bca3b72649a79e4245302df9ec06","水果1");
TestThread2 t2 = new TestThread2("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fi0.hdslb.com%2Fbfs%2Farticle%2F81dfe3b12316064e56a12ac52e82fab118b8e96e.jpg&refer=http%3A%2F%2Fi0.hdslb.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1614224630&t=8e54bca3b72649a79e4245302df9ec06","水果2");
TestThread2 t3 = new TestThread2("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fi0.hdslb.com%2Fbfs%2Farticle%2F81dfe3b12316064e56a12ac52e82fab118b8e96e.jpg&refer=http%3A%2F%2Fi0.hdslb.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1614224630&t=8e54bca3b72649a79e4245302df9ec06","水果3");
//这里是同时执行的,谁先下载完就显示谁
t1.start();
t2.start();
t3.start();
}
}
//下载器
class WebDownloader{
//下载方法
public void downloader(String url,String name){
try {
FileUtils.copyURLToFile(new URL(url),new File(name));
} catch (IOException e) {
e.printStackTrace();
System.out.println("IO异常,downloader方法出现问题!");
}
}
}
Runnable
package Thread;
//创建线程方式二:实现runnable接口,重写run()方法,执行线程需要丢入runnable接口实现类,调用start开启线程
public class TestThread3 implements Runnable {
@Override
public void run() {
//run方法线程体
for (int i = 0; i < 20; i++) {
System.out.println("我在看代码---"+i);
}
}
public static void main(String[] args) {
//main 线程,主线程
//创建runnable接口的实现类对象
TestThread3 testThread3 = new TestThread3();
// //创建线程对象,通过线程对象来开启我们的线程,代理
// Thread thread = new Thread(testThread3);
//
// thread.start();
new Thread(testThread3).start();
for (int i = 0; i <1000; i++) {
System.out.println("我在学习多线程--"+i);
}
}
}
买火车票例子
package Thread;
//多个线程同时操作一个对象
//买火车票的例子
//发现问题:多个线程操作同一个资源的情况下,线程不安全,数据紊乱
public class TestThread4 implements Runnable{
private int ticketNums=10;
@Override
public void run() {
while (true){
if (ticketNums<=0){break;}
//模拟延时
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"-->拿到了第"+ticketNums--+"票");
}
}
public static void main(String[] args) {
TestThread4 ticket = new TestThread4();
new Thread(ticket,"小明").start();
new Thread(ticket,"tom").start();
new Thread(ticket,"黄牛").start();
}
}
龟兔赛跑
package Thread;
//模拟龟兔赛跑
public class Race implements Runnable {
//胜利者
private static String winner;
@Override
public void run() {
for (int i = 0; i <=100; i++) {
//模拟兔子休息
if (Thread.currentThread().getName().equals("兔子")&& i%10==0){
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//判断比赛是否结束
boolean flag=gameOver(i);
if (flag){//比赛结束,停止程序
break;
}
System.out.println(Thread.currentThread().getName()+"-->跑了"+i+"步");
}
}
//判断是否完成比赛
private boolean gameOver(int steps){
//判断是否有胜利者
if(winner !=null) {//已经存在胜利者
return true;
}{
if(steps>=100){
winner=Thread.currentThread().getName();
System.out.println("winner is"+winner);
return true;
}
}
return false;
}
public static void main(String[] args) {
Race race=new Race();
new Thread(race,"兔子").start();
new Thread(race,"乌龟").start();
}
}
Callable
3,静态代理模式
package Thread;
//静态代理模式总结
//真实对象和代理对象都要实现同一个接口
//代理对象要代理真实角色
/**
* 好处:
* 代理对象可以做很多真实对象做不了的事情
* 真实对象专注做自己的事情
*/
public class StaticProxy {
public static void main(String[] args) {
You you = new You();
new Thread(()-> System.out.println("我爱你")).start();
new WeddingCompany(new You()).HappyMarry();
// WeddingCompany company = new WeddingCompany(new You());
// company.HappyMarry();
}
}
interface Marry{
void HappyMarry();
}
//真实角色
class You implements Marry{
@Override
public void HappyMarry() {
System.out.println("tom要结婚了,超级开心!");
}
}
//代理角色,帮助你结婚
class WeddingCompany implements Marry{
//代理谁->真实目标角色
private Marry target;
public WeddingCompany(Marry target) {
this.target = target;
}
@Override
public void HappyMarry() {
before();
this.target.HappyMarry();//这就是真实对象
after();
}
private void before() {
System.out.println("结婚之前,布置现场");
}
private void after() {
System.out.println("结婚之后,收尾款");
}
}
4,Lamda表达式
推导过程
package Lamda;
//推导Lamda表达式
public class Test1 {
//3,静态内部类
static class Like2 implements ILike{
@Override
public void lamda() {
System.out.println("i like lamda2 ");
}
}
public static void main(String[] args) {
ILike like = new Like();
like.lamda();
like = new Like2();
like.lamda();
//4,局部内部类
class Like3 implements ILike{
@Override
public void lamda() {
System.out.println("i like lamda3 ");
}
}
like = new Like3();
like.lamda();
//5,匿名内部类 没有类的名称,必须借助接口或者父类
like =new ILike(){
@Override
public void lamda() {
System.out.println("i like lamda4 ");
}
};
like.lamda();
//6,用lamda简化
like=()->{
System.out.println("i like lamda5");
};
like.lamda();
}
}
//1,定义一个函数式接口
interface ILike{
void lamda();
}
//2,实现类
class Like implements ILike{
@Override
public void lamda() {
System.out.println("i like lamda");
}
}
应用
package Lamda;
public class Test2 {
public static void main(String[] args) {
//1,lamda表达式
Ilove love=(int a)->{
System.out.println(" I love you -->"+a);
};
love.love(520);
//简化1:去掉参数类型
love=(a)->{
System.out.println(" I love you -->"+a);
};
love.love(521);
//简化2:简化括号
Ilove love1=null;
love1=a -> {
System.out.println(" I love you -->"+a);
};
love1.love(522);
//简化3:去掉花括号
Ilove love2=null;
love2=a->System.out.println(" I love you -->"+a);
love2.love(523);
}
}
interface Ilove{
void love(int a);
}
5,线程状态
package state;
//测试stop
//1,建议线程正常停止-->利用次数,不建议死循环
//2,建议使用标志位-->设置一个标志位
//3,不要使用stop或者destroy等过时或者不建议使用的方法
public class Test1 implements Runnable{
//1,设置一个标志位
private boolean flag=true;
@Override
public void run() {
int i=0;
while (flag){
System.out.println("run...Thread"+i++);
}
}
//2,设置一个公开的方法停止线程,转换标志位
public void stop(){
this.flag=false;
}
public static void main(String[] args) {
Test1 test1 = new Test1();
new Thread(test1).start();
for (int i = 0; i < 1000; i++) {
System.out.println("main"+i);
if (i==900){
//调用stop方法切换标志位,让线程停止
test1.stop();
System.out.println("线程该停止了");
}
}
}
}
1,线程休眠–sleep
倒计时
package state;
import java.text.SimpleDateFormat;
import java.util.Date;
//模拟倒计时
public class TestSleep2 {
public static void main(String[] args) {
// try {
// tenDwon();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
//打印系统当前时间
Date date = new Date(System.currentTimeMillis());//获取系统当前时间
while (true){
try {
Thread.sleep(1000);
System.out.println(new SimpleDateFormat("HH:mm:ss").format(date));
date = new Date(System.currentTimeMillis());//更新当前时间
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//模拟倒计时
public static void tenDwon() throws InterruptedException {
int num=10;
while (true){
Thread.sleep(1000);
System.out.println(num--);
if (num<=0){
break;
}
}
}
}
2,线程礼让–yield
package state;
//礼让线程
//礼让不一定成功,看cpu心情
public class TestYield {
public static void main(String[] args) {
MyYield myYield = new MyYield();
new Thread(myYield,"a").start();
new Thread(myYield,"b").start();
}
}
class MyYield implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"线程开始执行");
Thread.yield();//礼让
System.out.println(Thread.currentThread().getName()+"线程停止执行");
}
}
3,线程强制执行–join
package state;
public class TestJoin implements Runnable {
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
System.out.println("线程vip来了"+i);
}
}
public static void main(String[] args) throws InterruptedException {
//启动线程
TestJoin join = new TestJoin();
Thread thread = new Thread(join);
thread.start();
//主线程
for (int i = 0; i < 500; i++) {
if (i==200){
thread.join();//插队
}
System.out.println("main"+i);
}
}
}
4,线程状态观测state
package state;
public class TestState {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(()->{
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("");
});
//观察状态
Thread.State state = thread.getState();
System.out.println(state);//new
//观察启动后
thread.start();//启动线程
state=thread.getState();
System.out.println(state);//run\
while (state!=Thread.State.TERMINATED){//只要线程不终止,就一直输出状态
Thread.sleep(100);
state=thread.getState();//更新线程状态
System.out.println(state);//输出状态
}
}
}
5,线程优先级priority
package state;
//测试线程的优先级
public class TestPriority {
public static void main(String[] args) {
//主线程默认优先级
System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
MyPriority priority = new MyPriority();
Thread t1 = new Thread(priority);
Thread t2= new Thread(priority);
Thread t3 = new Thread(priority);
Thread t4 = new Thread(priority);
Thread t5 = new Thread(priority);
Thread t6 = new Thread(priority);
//先设置优先级,再启动
t1.start();
t2.setPriority(1);
t2.start();
t3.setPriority(4);
t3.start();
t4.setPriority(Thread.MAX_PRIORITY);
t4.start();
t5.setPriority(7);
t5.start();
t6.setPriority(8);
t6.start();
}
}
class MyPriority implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
}
}
6,守护线程daemon
package state;
//测试守护线程
//上帝守护你
public class TestDaemon {
public static void main(String[] args) {
God god = new God();
You you = new You();
Thread thread = new Thread(god);
thread.setDaemon(true);//默认是false表示是用户线程,正常的线程都是用户线程
thread.start();//上帝, 守护线程启动
new Thread(you).start();//你 ,用户线程启动
}
}
//上帝
class God implements Runnable{
@Override
public void run() {
while (true){
System.out.println("上帝保佑你");
}
}
}
//你
class You implements Runnable{
@Override
public void run() {
for (int i = 0; i < 36500; i++) {
System.out.println("你一生都开心的活着");
}
System.out.println("=========goodbye world========");
}
}
6,线程同步synchronize
多个线程操作同一个资源(并发)
1,三大不安全举例
一:
package syn;
//不安全买票
//线程不安全,有-1
public class UnSafeBuyTicket {
public static void main(String[] args) {
BuyTicket station = new BuyTicket();
new Thread(station,"小明").start();
new Thread(station,"tom").start();
new Thread(station,"mary").start();
}
}
class BuyTicket implements Runnable{
private int ticketNum=10;
boolean flag=true;//外部停止方式
@Override
public void run() {
//买票
while(flag){
buy();
}
}
private void buy(){
//判断是否有票
if (ticketNum<=0){
flag=false;
return;
}
//模拟延时
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
//买票
System.out.println(Thread.currentThread().getName()+"拿到"+ticketNum--);
}
}
二:
package syn;
//不安全的取钱
//两个人去银行取钱,账户
public class UnSafeBuyBank {
public static void main(String[] args) {
//账户
Account account = new Account(100, "结婚基金");
Drawing you = new Drawing(account, 50, "你");
Drawing girlFriend = new Drawing(account, 100, "girlFriend");
you.start();
girlFriend.start();
}
}
//账户
class Account{
int money;//余额
String name;
public Account(int money, String name) {
this.money = money;
this.name = name;
}
}
//银行:模拟取款
class Drawing extends Thread{
Account account;//账户
//去了多少钱
int drawingMoney;
//现在手里存有多少钱
int nowMoney;
public Drawing(Account account, int drawingMoney, String name) {
super(name);
this.account = account;
this.drawingMoney = drawingMoney;
}
@Override
public void run() {
//判断有没有钱
if (account.money-drawingMoney<=0){
System.out.println(Thread.currentThread().getName()+"钱不够,取不了");
return;
}
//模拟延时
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//卡内余额=余额-你去的钱
account.money=account.money-drawingMoney;
//你手里的钱
nowMoney=nowMoney+drawingMoney;
System.out.println(account.name +"余额为:"+account.money);
//Thread.currentThread().getName()=this.getName()
System.out.println(this.getName()+"手里的钱"+nowMoney);
}
}
三:
package syn;
import java.util.ArrayList;
import java.util.List;
public class UnSafeBuyList {
public static void main(String[] args) {
List<String> list= new ArrayList<String>();
for (int i = 0; i < 10000; i++) {
new Thread(
()->{
list.add(Thread.currentThread().getName());
}
).start();
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(list.size());
}
}
2,同步方法
一:解决买票
package syn;
//不安全买票
//线程不安全,有-1
public class UnSafeBuyTicket {
public static void main(String[] args) {
BuyTicket station = new BuyTicket();
new Thread(station,"小明").start();
new Thread(station,"tom").start();
new Thread(station,"mary").start();
}
}
class BuyTicket implements Runnable{
private int ticketNum=10;
boolean flag=true;//外部停止方式
@Override
public void run() {
//买票
while(flag){
buy();
}
}
//synchronized 同步方法,锁的是this
private synchronized void buy(){
//判断是否有票
if (ticketNum<=0){
flag=false;
return;
}
//模拟延时
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
//买票
System.out.println(Thread.currentThread().getName()+"拿到"+ticketNum--);
}
}
二:银行取钱解决
package syn;
//不安全的取钱
//两个人去银行取钱,账户
public class UnSafeBuyBank {
public static void main(String[] args) {
//账户
Account account = new Account(1000, "结婚基金");
Drawing you = new Drawing(account, 50, "你");
Drawing girlFriend = new Drawing(account, 100, "girlFriend");
you.start();
girlFriend.start();
}
}
//账户
class Account{
int money;//余额
String name;
public Account(int money, String name) {
this.money = money;
this.name = name;
}
}
//银行:模拟取款
class Drawing extends Thread{
Account account;//账户
//去了多少钱
int drawingMoney;
//现在手里存有多少钱
int nowMoney;
public Drawing(Account account, int drawingMoney, String name) {
super(name);
this.account = account;
this.drawingMoney = drawingMoney;
}
@Override
public void run() {
synchronized (account){
//判断有没有钱
if (account.money-drawingMoney<=0){
System.out.println(Thread.currentThread().getName()+"钱不够,取不了");
return;
}
//模拟延时
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//卡内余额=余额-你去的钱
account.money=account.money-drawingMoney;
//你手里的钱
nowMoney=nowMoney+drawingMoney;
System.out.println(account.name +"余额为:"+account.money);
//Thread.currentThread().getName()=this.getName()
System.out.println(this.getName()+"手里的钱"+nowMoney);
}
}
}
三:解决集合
package syn;
import java.util.ArrayList;
import java.util.List;
public class UnSafeBuyList {
public static void main(String[] args) {
List<String> list= new ArrayList<String>();
for (int i = 0; i < 10000; i++) {
new Thread(
()->{synchronized (list){
list.add(Thread.currentThread().getName());
}
}
).start();
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(list.size());
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-V62XV27i-1616484175800)(https://gitee.com/beekim/pic-box/raw/master/img/image-20210129183554137.png)]
JUC(本身就是安全的):
package syn;
import java.util.concurrent.CopyOnWriteArrayList;
//测试JUC安全类型的集合
public class TestJUC {
public static void main(String[] args) {
CopyOnWriteArrayList<String> list =new CopyOnWriteArrayList<String>();
for (int i = 0; i < 1000; i++) {
new Thread(
()->{
list.add(Thread.currentThread().getName());
}
).start();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(list.size());
}
}
7,死锁
package lock;
//死锁:两个线程互相抱着对方需要的资源,然后形成僵持
public class DeadLock {
public static void main(String[] args) {
Makeup g1 = new Makeup(0, "小红");
Makeup g2 = new Makeup(0, "小蓝");
g1.start();
g2.start();
}
}
//口红
class LipStick{
}
//镜子
class Mirror{
}
class Makeup extends Thread{
//需要的资源只有一份,用static来保证只有一份
static LipStick lipStick=new LipStick();
static Mirror mirror= new Mirror();
int choice;
String girlName;
public Makeup(int choice, String girlName) {
this.choice = choice;
this.girlName = girlName;
}
@Override
public void run() {
//化妆
try {
makeup();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//互相持有对方的锁,需要拿到对方的资源
private void makeup() throws InterruptedException {
if (choice==0){
synchronized (lipStick){//获得口红的锁
System.out.println(this.girlName+"获得口红的锁");
Thread.sleep(1000);
} synchronized (mirror){//1s之后想获得镜子的锁
System.out.println(this.girlName+"获得镜子的锁");
}
}else{
synchronized (mirror){//获得镜子的锁
System.out.println(this.girlName+"获得镜子的锁");
Thread.sleep(2000);
}
synchronized (lipStick){//2s之后想获得口红的锁
System.out.println(this.girlName+"获得口红的锁");
}
}
}
}
8,Lock锁
package lock;
import java.util.concurrent.locks.ReentrantLock;
//测试Lock锁
public class TestLock {
public static void main(String[] args) {
TestLock2 lock2 = new TestLock2();
new Thread(lock2).start();
new Thread(lock2).start();
new Thread(lock2).start();
}
}
class TestLock2 implements Runnable{
int ticketNums=10;
//定义Lock锁
private final ReentrantLock lock= new ReentrantLock();
@Override
public void run() {
while (true){
try {
lock.lock();//加锁
if (ticketNums>0){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(ticketNums--);
}else {
break;
}
}finally {
//解锁
lock.unlock();
}
}
}
}
9,线程协作
1,生产者与消费者
2,管程法
package lock;
//测试生产者与消费者
//生产者,消费者,产品,缓冲区
public class TestPC {
public static void main(String[] args) {
SynContainer container=new SynContainer();
new Productor(container).start();
new Consumer(container).start();
}
}
//生产者
class Productor extends Thread{
SynContainer container;
public Productor(SynContainer container) {
this.container = container;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("生产了"+i+"只鸡");
container.push(new Chicken(i));
}
}
}
//消费者
class Consumer extends Thread{
SynContainer container;
public Consumer(SynContainer container) {
this.container = container;
}
//消费
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("消费了-->"+container.pop().id+"只鸡");
}
}
}
//产品
class Chicken {
int id;//产品编号
public Chicken(int i) {
}
}
//缓冲区
class SynContainer {
//需要一个容器大小
Chicken[] chickens =new Chicken[10];
//容器计数器
int count=0;
//生产者放入产品
public synchronized void push(Chicken chicken){
//如果容器满了,就需要等待消费者消费
if (count==chickens.length){
//通知消费者消费,生产等待
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//如果没有满,我们就需要丢入产品
chickens[count]=chicken;
count++;
//可以通知消费之消费了
this.notifyAll();
}
//消费者消费产品
public synchronized Chicken pop(){
//判断能否消费
if (count==0){
//等待生产者生产,消费者等待
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//如果可以消费
count--;
Chicken chicken=chickens[count];
//吃完了,通知生产者生产
this.notifyAll();
return chicken;
}
}
3,信号灯法
!代码有问题!
package lock;
//信号灯法,标志位解决
public class TestPC2 {
public static void main(String[] args) {
TV tv =new TV();
new Player(tv).start();
new Watcher(tv).start();
}
}
//生产者--》演员
class Player extends Thread{
TV tv;
public Player(TV tv) {
this.tv = tv;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
if (i%2==0){
this.tv.play("快乐大本营播放中");
}else{
this.tv.play("抖音广告播放中");
}
}
}
}
//消费者--》观众
class Watcher extends Thread{
TV tv;
public Watcher(TV tv) {
this.tv = tv;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
tv.watch();
}
}
}
//产品--》节目
class TV{
//演员表演,观众等待 T
//观众观看,演员等待 F
String voice;//表演节目
boolean flag=true;
//表演
public synchronized void play(String voice){
if (!flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("演员表演完了:"+voice);
//通知观众观看
this.notifyAll();
this.voice=voice;
this.flag=!this.flag;
}
//观看
public synchronized void watch(){
if (flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("观看了:"+voice);
//通知演员表演
this.notifyAll();
this.flag=!this.flag;
}
}
}
4,线程池
package lock;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
//测试线程池
public class TestPool {
public static void main(String[] args) {
//1,创建线程服务,创建线程池
//newFixedThreadPool参数为:线程池大小
ExecutorService service= Executors.newFixedThreadPool(10);
//执行
service.execute(new MyThread());
service.execute(new MyThread());
service.execute(new MyThread());
service.execute(new MyThread());
//关闭链接
service.shutdown();
}
}
class MyThread implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
}
注:本笔记于b站up主“遇见狂神说”
https://www.bilibili.com/video/BV1V4411p7EF
处学习记录,仅供学习与参考