多线程(multithreading),是指从软件或者硬件上实现多个线程并发执行的技术。具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程,进而提升整体处理性能。具有这种能力的系统包括对称多处理机、多核心处理器以及芯片级多处理或同时多线程处理器。在一个程序中,这些独立运行的程序片段叫作“线程”(Thread),利用它编程的概念就叫作“多线程处理”
自定义线程类继承Thread类
public class TestThread1 extends Thread {
@Override
public void run() {
for (int i = 0; i < 20; i++) {
System.out.println("我在看代码--" + i);
}
}
重写run()方法,编写线程执行体
@Override
public void run() {
....
}
创建线程对象,调用start()方法启动线程
public static void main(String[] args) {
//main线程 主线程
//创建一个线程对象
TestThread1 testThread1 = new TestThread1();
//调用start()方法开启线程
testThread1.start();
for (int i = 0; i < 200; i++) {
System.out.println("我在学习多线程----" + i);
}
}
利用多线程编写一个图片下载工具 需要用到 commons-io-2.8.0.jar
//下载器
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方法出现问题");
}
}
}
public class TestThread2 extends Thread {
private String url = null; //网络图片地址
private String name = null; // 保存的图片名
public TestThread2(String url, String name) {
this.url = url;
this.name = name;
}
@Override
public void run() {
WebDownloader webDownloader = new WebDownloader();
webDownloader.downloader(url, name);
System.out.println("下载的文件名:" + name);
}
public static void main(String[] args) {
TestThread2 testThread2 = new TestThread2("https://img2020.cnblogs.com/blog/2182263/202102/2182263-20210227230703958-100861443.png", "1.jpg");
TestThread2 testThread3 = new TestThread2("https://img2020.cnblogs.com/blog/2182263/202102/2182263-20210227230703958-100861443.png", "2.jpg");
TestThread2 testThread4 = new TestThread2("https://img2020.cnblogs.com/blog/2182263/202102/2182263-20210227230703958-100861443.png", "3.jpg");
testThread2.start();
testThread3.start();
testThread4.start();
}
}
创建线程方法二:Runnable
//创建线程方式2:实现Runnable接口,重写run方法,执行线程需要丢入Runnable接口实现类,调用start方法
public class TestThread3 implements Runnable {
@Override
public void run() {
for (int i = 0; i < 20; i++) {
System.out.println("我在看代码--" + i);
}
}
public static void main(String[] args) {
//main线程 主线程
//创建一个线程对象
TestThread3 testThread3 = new TestThread3();
//执行线程需要丢入Runnable接口实现类
Thread thread = new Thread(testThread3);
//调用start()方法开启线程
thread.start();
for (int i = 0; i < 200; i++) {
System.out.println("我在学习多线程----" + i);
}
}
}
总结:继承Thread类
实现Runnable接口
龟兔赛跑案例
public class Race implements Runnable {
private static String winner = null;
@Override
public void run() {
for (int i = 0; i <= 100; i++) {
//模拟兔子睡觉
if(Thread.currentThread().getName().equals("兔子")&& i%10==0){
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//判断比赛是否结束
boolean flag = gameOver(i);
if (flag) {
// System.out.println("比赛结束");
break;
}
System.out.println(Thread.currentThread().getName() + "跑了" + i + "步");
}
}
public static void main(String[] args) {
Race race = new Race();
new Thread(race, "兔子").start();
new Thread(race, "乌龟").start();
}
public 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;
}
}
实现Callable接口
实现Callable接口,需要返回值类型
重写call方法,需要抛出异常
public class TestCallable implements Callable<Boolean> {
private String url = null; //网络图片地址
private String name = null; // 保存的图片名
public TestCallable(String url, String name) {
this.url = url;
this.name = name;
}
@Override
public Boolean call() {
WebDownloader webDownloader = new WebDownloader();
webDownloader.downloader(url, name);
System.out.println("下载的文件名:" + name);
return true;
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
TestCallable testThread2 = new TestCallable("https://img2020.cnblogs.com/blog/2182263/202102/2182263-20210227230703958-100861443.png", "1.jpg");
TestCallable testThread3 = new TestCallable("https://img2020.cnblogs.com/blog/2182263/202102/2182263-20210227230703958-100861443.png", "2.jpg");
TestCallable testThread4 = new TestCallable("https://img2020.cnblogs.com/blog/2182263/202102/2182263-20210227230703958-100861443.png", "3.jpg");
//创建执行服务:
ExecutorService service = Executors.newFixedThreadPool(3);
//提交执行:
Future<Boolean> result1 = service.submit(testThread2);
Future<Boolean> result2 = service.submit(testThread3);
Future<Boolean> result3 = service.submit(testThread4);
//获取结果
boolean r1 = result1.get();
boolean r2 = result2.get();
boolean r3 = result3.get();
System.out.println(r1);
System.out.println(r2);
System.out.println(r3);
//关闭服务
service.shutdownNow();
}
}
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方法出现问题");
}
}
静态代理案例
//静态代理模式
//真实对象和代理对象都要实现同一个接口
//代理对象要代理真实角色
//好处:
// 代理对象可以做很多真实对象做不了的事情
// 真实对象专注做自己的事情
public class StaticProxy {
public static void main(String[] args) {
You you = new You(); //你要结婚
new Thread(() -> System.out.println("i my ")).start();
new WwddingCompany(new You()).HappyMarry();
}
}
interface Marry {
void HappyMarry();
}
class You implements Marry {
@Override
public void HappyMarry() {
System.out.println("恭喜,恭喜,新婚快乐");
}
}
class WwddingCompany implements Marry {
private Marry target;
public WwddingCompany(Marry target) {
this.target = target;
}
@Override
public void HappyMarry() {
before();
this.target.HappyMarry(); //真实对象
after();
}
private void after() {
System.out.println("送入洞房,结账难受");
}
private void before() {
System.out.println("结婚之前,布置场景,大家开心");
}
}
Lamda表达式
为什么要使用lambda表达式
推到过程
package Lambda;
// 推到lambda表达式
public class TestLambda1 {
//3 静态内部类
static class Lk1 implements Like {
@Override
public void lambda() {
System.out.println("i like 2");
}
}
public static void main(String[] args) {
Like lk = new Lk();
lk.lambda();
lk = new Lk1();
lk.lambda();
//4 局部内部类
class Lk2 implements Like {
@Override
public void lambda() {
System.out.println("i like 3");
}
}
lk = new Lk2();
lk.lambda();
// 5 匿名内部类,没有类名,必须借助接口或者父类
lk = new Like() {
@Override
public void lambda() {
System.out.println("i like 4");
}
};
lk.lambda();
//6 用lambda简化
lk = () -> {
System.out.println("i like 5");
};
lk = () -> System.out.println("i like 5");
lk.lambda();
}
}
//1 定义一个函数式接口
interface Like {
void lambda();
}
// 2 实现类
class Lk implements Like {
@Override
public void lambda() {
System.out.println("i like 1");
}
}
方法 | 描述 |
---|---|
setPriority(int newPriority) | 更改线程的优先级 |
static void sleep(long millis) | 在指定的毫秒数内让当前正在执行的线程休眠 |
void join() | 等待该线程终止 |
static void yield() | 暂停当前正在执行的线程对象,并执行其他线程 |
void interrupt() | 终极现场,别用这个方式 |
boolean isAlive() | 测试线程是否处于活动状态 |
//测试stop
//1.建议线程正常停止--->利用慈湖,不建议死循环
//2.建议使用标志位-->设置一个标志位
//3.不要使用stop或者destroy等过时或者JDK不建议使用的方法
public class TestStop 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) {
TestStop testStop = new TestStop();
new Thread(testStop).start();
for (int i = 0; i < 1000; i++) {
System.out.println("main" + i);
if (i == 900) {
//调用stop方法切换标志位,让线程停止
testStop.stop();
System.out.println("线程该停止了");
}
}
}
}
模拟倒计时
public static void tenDown() throws InterruptedException {
int num = 10;
while (true) {
Thread.sleep(1000);
System.out.println(num--);
if (num < 0) {
break;
}
}
}
模拟打印系统时间
public static void main(String[] args) throws InterruptedException {
//打印当前时间
Date startTime = new Date(System.currentTimeMillis());
while (true) {
Thread.sleep(1000);
System.out.println(new SimpleDateFormat("HH:mm:ss").format(startTime));
startTime = new Date(System.currentTimeMillis());
}
}
class MyYield implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "开始...");
Thread.yield(); //
System.out.println(Thread.currentThread().getName() + "结束...");
}
}
Join合并线程,待此线程执行完成后,在执行其他线程
public static void main(String[] args) throws InterruptedException {
TestJoin myJoin = new TestJoin();
Thread thread = new Thread(myJoin);
thread.start();
for (int i = 0; i < 500; i++) {
if (i == 200) {
thread.join();//
}
System.out.println("main" + i);
}
}
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
System.out.println( "VIP开始..." + i);
}
}
线程状态。线程可以处于以下状态之一:
NEW
RUNNABLE
BLOCKED
WAITING
TIMED_WAITING
TERMINATED
//观察测试线程状态
public class TestState {
public static void main(String[] args) {
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) {
try {
Thread.sleep(100);
state = thread.getState();
System.out.println(state);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
thread.start();
}
}
import java.util.TreeMap;
public class TestPriority {
public static void main(String[] args) {
//主线程默认优先级
System.out.println(Thread.currentThread().getName() + "-->" + Thread.currentThread().getPriority());
MyPriority myPriority = new MyPriority();
Thread t1 = new Thread(myPriority);
Thread t2 = new Thread(myPriority);
Thread t3 = new Thread(myPriority);
Thread t4 = new Thread(myPriority);
Thread t5 = new Thread(myPriority);
//先设置,在启动
t1.start();
t2.setPriority(1);
t2.start();
t3.setPriority(4);
t3.start();
t4.setPriority(Thread.MAX_PRIORITY);
t4.start();
}
}
class MyPriority implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "-->" + Thread.currentThread().getPriority());
}
}
public class TestDaemon {
public static void main(String[] args) {
God god = new God();
Yon yon = new Yon();
Thread thread = new Thread(god);
thread.setDaemon(true); //默认是false表示用户线程
thread.start(); //守护线程启动
new Thread(yon).start();//用户线程启动
}
}
class God implements Runnable {
@Override
public void run() {
while (true) {
System.out.println("上帝保佑着你");
}
}
}
class Yon implements Runnable {
@Override
public void run() {
for (int i = 0; i < 36500; i++) {
System.out.println("你一生都开心的或者");
}
System.out.println("=======goodbye! world!");
}
}
并发:同一个线程被多个事务调用
队列和锁
synchronized(Obj){}
案例
//不安全的买票
public class UnsafeBuyTicket {
public static void main(String[] args) {
BuyTicket buyTicket = new BuyTicket();
new Thread(buyTicket, "难受的我").start();
new Thread(buyTicket, "开心的你").start();
new Thread(buyTicket, "可恶的黄牛").start();
}
}
class BuyTicket implements Runnable {
private int ticketName = 10;
boolean flag = true;
@Override
public void run() {
while (flag) {
try {
buy();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private synchronized void buy() throws InterruptedException {
if (ticketName <= 0) {
flag = false;
return;
}
Thread.sleep(3000);
System.out.println(Thread.currentThread().getName() + "-->买到了" + ticketName--);
}
}
public class UnsafeBank {
public static void main(String[] args) {
Account account = new Account(100, "生活费");
Drawing my = new Drawing(account, 50, "我");
Drawing you = new Drawing(account, 100, "你");
my.start();
you.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 myMoney;
int noMoney;
public Drawing(Account account, int myMoney, String name) {
super(name);
this.account = account;
this.myMoney = myMoney;
}
//synchronized默认锁的是this
public void run() {
synchronized (account){
if (account.money - myMoney < 0) {
System.out.println(Thread.currentThread().getName() + "钱不够。。。。");
return;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
account.money = account.money - myMoney;
noMoney = noMoney + myMoney;
System.out.println(account.name + "余额为:" + account.money);
System.out.println(this.getName() + "手里的钱" + noMoney);
}
}
}
public class UnsafeList {
public static void main(String[] args) throws InterruptedException {
List<String> list = new ArrayList<String>();
for (int i = 0; i < 10000; i++) {
new Thread(() -> {
synchronized (list) {
list.add(Thread.currentThread().getName());
}
}).start();
}
Thread.sleep(1000);
System.out.println(list.size());
}
}
JUC扩充
//测试JUC安全类型的集合
public class TestJUC {
public static void main(String[] args) {
CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<String>();
for (int i = 0; i < 10000; i++) {
new Thread(() -> {
list.add(Thread.currentThread().getName());
}).start();
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(list.size());
}
}
死锁:某一个同步块同时拥有“两个以上对象的锁”时,就可能会发生“死锁”的问题
产生死锁的四个必要条件:
解决办法:上面的四个必要条件,我们只要想办法打破一个或多个就可以避免死锁的发送
死锁:
//多个线程互相持有对方需要的资源,形成僵持
public class DeadLock {
public static void main(String[] args) {
Makeup top1 = new Makeup(0, "1号选手");
Makeup top2 = new Makeup(1, "2号选手");
top1.start();
top2.start();
}
}
class Lipstick {
}
class Mirror {
}
class Makeup extends Thread {
static Lipstick lipstick = new Lipstick();
static Mirror mirror = new Mirror();
int choice;
String girName;
Makeup(int choice, String girName) {
this.choice = choice;
this.girName = girName;
}
@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.girName + "获得口红的锁");
Thread.sleep(1000);
synchronized (mirror) {
System.out.println(this.girName + "获得镜子的锁");
}
}
} else {
synchronized (mirror) { //获得口红锁
System.out.println(this.girName + "获得镜子的锁");
Thread.sleep(2000);
synchronized (lipstick) {
System.out.println(this.girName + "获得口红的锁");
}
}
}
}
}
修改后
//多个线程互相持有对方需要的资源,形成僵持
public class DeadLock {
public static void main(String[] args) {
Makeup top1 = new Makeup(0, "1号选手");
Makeup top2 = new Makeup(1, "2号选手");
top1.start();
top2.start();
}
}
class Lipstick {
}
class Mirror {
}
class Makeup extends Thread {
static Lipstick lipstick = new Lipstick();
static Mirror mirror = new Mirror();
int choice;
String girName;
Makeup(int choice, String girName) {
this.choice = choice;
this.girName = girName;
}
@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.girName + "获得口红的锁");
Thread.sleep(1000);
}
synchronized (mirror) {
System.out.println(this.girName + "获得镜子的锁");
}
} else {
synchronized (mirror) { //获得口红锁
System.out.println(this.girName + "获得镜子的锁");
Thread.sleep(2000);
}
synchronized (lipstick) {
System.out.println(this.girName + "获得口红的锁");
}
}
}
}
Lock(锁):可重入锁
通过显示定义同步锁对象来实现同步。同步锁使用Lock对象充当
public class TestLock {
public static void main(String[] args) {
TestLock2 testLock2 = new TestLock2();
new Thread(testLock2).start();
new Thread(testLock2).start();
new Thread(testLock2).start();
}
}
class TestLock2 implements Runnable {
int ticketName = 10;
//定义Lock锁
private final ReentrantLock lock = new ReentrantLock();
@Override
public void run() {
while (true) {
try {
lock.lock();//枷锁
if (ticketName > 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(ticketName--);
} else {
break;
}
} finally {
//解锁
lock.unlock();
}
}
}
}
synchronized与Lock的对比
线程协作
应用场景:生产者消费者模式
Java提供了及格方法解决线程之间的通信问题
方法 | 描述 |
---|---|
wait() | 表示线程一直等待,知道其他线程通知,与sleep不同,会释放锁 |
wait(long timeout) | 指定等待的毫秒数 |
notify() | 唤醒一个处于等待状态的线程 |
notifyAll | 唤醒同一个对象所有调用wait()方法的线程,优先级别高的线程优先调度 |
生产者消费者模式--》利用缓冲区解决:管程法
//测试:生产者消费者模式--》利用缓冲区解决:管程法
public class TestPC {
public static void main(String[] args) {
SynContainer synContainer = new SynContainer();
new Productor(synContainer).start();
new Consumer(synContainer).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++) {
try {
container.push(new Chicken(i));
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("生产了" + 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++) {
try {
System.out.println("消费类了--->" + container.pop().id + "只鸡");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Chicken {
int id;
public Chicken(int id) {
this.id = id;
}
}
//缓冲区
class SynContainer {
//需要容器的大小
Chicken[] chickens = new Chicken[10];
int count = 0;
//生产者放入商品
public synchronized void push(Chicken chicken) throws InterruptedException {
//如果容器满了,就需要等待消费
if (count == chickens.length) {
this.wait();
}
chickens[count] = chicken;
count++;
//生产好了,可以消费了
this.notify();
}
//消费者取出商品
public synchronized Chicken pop() throws InterruptedException {
if (count == 0) {
this.wait();
}
//如果可以消费
count--;
Chicken chicken = chickens[count];
//吃完了
this.notify();
return chicken;
}
}
测试测试生产消费者模式2:信号灯法,标志位解决
//测试测试生产消费者模式2:信号灯法,标志位解决
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 = new Tv();
public Player(Tv tv) {
this.tv = tv;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
if (i % 2 == 0) {
try {
this.tv.play("天天向上");
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
try {
this.tv.play("GOGO Baobab");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
//消费者
class Watcher extends Thread {
Tv tv = new Tv();
public Watcher(Tv tv) {
this.tv = tv;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
try {
tv.watch();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//产品--节目
class Tv {
//演员表演的时候观众等待
//观众观看,演员等待
String voice; //节目
boolean flag = true;
//表演
public synchronized void play(String voice) throws InterruptedException {
if (!flag) {
this.wait();
}
System.out.println("演员表演完了:" + voice);
//通知观众观看
this.notify();//通知唤醒
this.voice = voice;
this.flag = !this.flag;
}
//观看
public synchronized void watch() throws InterruptedException {
if (flag) {
this.wait();
}
System.out.println("观众观看了" + voice);
//通知演员表演
this.notify();
this.flag = !this.flag;
}
}
线程池:
使用线程池的好处
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
//测试线程池
public class TestPool {
public static void main(String[] args) {
//1.创建线程池
//线程池大小
ExecutorService service = Executors.newFixedThreadPool(10);
service.execute(new MyPool());
service.execute(new MyPool());
service.execute(new MyPool());
service.execute(new MyPool());
//关闭连接
service.shutdownNow();
}
}
class MyPool implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() );
}
}
总结:
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class ThreadNew {
public static void main(String[] args) {
new MyThread1().start();
new Thread(new MyThread2()).start();
FutureTask<Integer> futureTask = new FutureTask<Integer>(new MyThread3());
new Thread(futureTask).start();
try {
System.out.println(futureTask.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
//继承Thread类
class MyThread1 extends Thread {
@Override
public void run() {
System.out.println("MyThread1");
}
}
//实现Runnable接口
class MyThread2 implements Runnable {
@Override
public void run() {
System.out.println("MyThread2");
}
}
//实现Callable接口
class MyThread3 implements Callable<Integer> {
@Override
public Integer call() throws Exception {
System.out.println("MyThread3");
return 100;
}
}
原文:https://www.cnblogs.com/huangwenpeng/p/14462952.html