多线程
多线程
Java.Thread
进程和线程关系及区别
1.定义
进程是具有一定独立功能的程序关于某个数据集合上的一次运行活动,进程是系统进行资源分配和调度的一个独立单位.
线程是进程的一个实体,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位.线程自己基本上不拥有系统资源,只拥有一点在运行中必不可少的资源(如程序计数器,一组寄存器和栈),但是它可与同属一个进程的其他的线程共享进程所拥有的全部资源.
2.关系
一个线程可以创建和撤销另一个线程;同一个进程中的多个线程之间可以并发执行.
相对进程而言,线程是一个更加接近于执行体的概念,它可以与同进程中的其他线程共享数据,但拥有自己的栈空间,拥有独立的执行序列。
3.区别
进程和线程的主要差别在于它们是不同的操作系统资源管理方式。进程有独立的地址空间,一个进程崩溃后,在保护模式下不会对其它进程产生影响,而线程只是一个进程中的不同执行路径。线程有自己的堆栈和局部变量,但线程之间没有单独的地址空间,一个线程死掉就等于整个进程死掉,所以多进程的程序要比多线程的程序健壮,但在进程切换时,耗费资源较大,效率要差一些。但对于一些要求同时进行并且又要共享某些变量的并发操作,只能用线程,不能用进程。
根本区别:进程是操作系统资源分配的基本单位,而线程是任务调度和执行的基本单位
-
简而言之,一个程序至少有一个进程,一个进程至少有一个线程.
-
线程的划分尺度小于进程,使得多线程程序的并发性高。
-
另外,进程在执行过程中拥有独立的内存单元,而多个线程共享内存,从而极大地提高了程序的运行效率。
-
线程在执行过程中与进程还是有区别的。每个独立的线程有一个程序运行的入口、顺序执行序列和程序的出口。但是线程不能够独立执行,必须依存在应用程序中,由应用程序提供多个线程执行控制。
-
从逻辑角度来看,多线程的意义在于一个应用程序中,有多个执行部分可以同时执行。但操作系统并没有将多个线程看做多个独立的应用,来实现进程的调度和管理以及资源分配。这就是进程和线程的重要区别。
4.优缺点
线程和进程在使用上各有优缺点:线程执行开销小,但不利于资源的管理和保护;而进程正相反。同时,线程适合于在SMP机器上运行,而进程则可以跨机器迁移。
线程创建
Thread class(重点)
- 自定义线程类继承Thread
- 重写 run() 方法,编写线程执行体
- 创建线程对象,调用 start() 方法启动线程
package demo01;public class TestThread extends Thread{ @Override public void run() { //run方法线程体 for (int i = 0; i < 200; i++) { System.out.println("git"+i); } } public static void main(String[] args) { //main线程,主线程TestThread testThread1 = new TestThread(); //调用start()方法开启线程 testThread1.start(); for (int i = 0; i < 1000; i++) { System.out.println("study"+i); } }}
网络图片下载练习
package demo01;import org.apache.commons.io.FileUtils;import java.io.File;import java.io.IOException;import java.net.URL;public class TestThread02 extends Thread{ private String url; private String name; public TestThread02(String url,String name){ this.name=name; this.url=url; } //下载图片线程的执行体 @Override public void run() { WebDownloader webDownloader =new WebDownloader(); webDownloader.downloader(url,name); System.out.println("下载的文件名:"+name); } public static void main(String[] args) { //创建线程对象 TestThread02 t1 = new TestThread02("https://static.nike.com/a/images/c_limit,w_592,f_auto/t_product_v1/044c17f1-6131-4352-9dc8-f1ccef6b8ace/af1-1-%E7%94%B7%E5%AD%90%E8%BF%90%E5%8A%A8%E9%9E%8B-fM9NKw.jpg","1.jpg"); TestThread02 t2 = new TestThread02("https://static.nike.com/a/images/c_limit,w_592,f_auto/t_product_v1/7d59c04b-49fd-4904-b8e2-03fab7102ee4/blazer-mid-77-%E5%A5%B3%E5%AD%90%E8%BF%90%E5%8A%A8%E9%9E%8B-sTd61w.jpg","2.jpg"); TestThread02 t3 = new TestThread02("https://static.nike.com/a/images/c_limit,w_592,f_auto/t_product_v1/e056586b-2660-4d96-b9eb-4e68f19a2312/lebron-18-nrg-%E5%A4%A7%E7%AB%A5%E7%AF%AE%E7%90%83%E7%AB%A5%E9%9E%8B-5BKwSZ.jpg","3.jpg"); 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 demo01;//实现runnable接口,重写run方法,执行线程需要丢入runnable接口实现类,调用start方法public class TestThread03 implements Runnable{ @Override public void run() { //run方法线程体 for (int i = 0; i < 200; i++) { System.out.println("git"+i); } } public static void main(String[] args) { //创建runnable接口的实现类对象TestThread03 testThread03 = new TestThread03(); //创建线程对象,通过线程对象来开启我们的线程,代理 //Thread thread = new Thread(testThread03); //thread.start(); new Thread(testThread03).start(); for (int i = 0; i < 1000; i++) { System.out.println("study"+i); } }}
并发问题
package demo01;import oop.demo07.Test;//多线程同时操作同一个对象public class TestThread04 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()+"get"+ticketNums--); } } public static void main(String[] args) { TestThread04 ticket = new TestThread04(); new Thread(ticket,"ming").start(); new Thread(ticket,"huang").start(); new Thread(ticket,"niu").start(); }}
龟兔赛跑练习
package demo01;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("tuzi") && i%10==0){ try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } //判断比赛是否结束 boolean flag = gameOver(i); if (flag){ break; } System.out.println(Thread.currentThread().getName()+"running"+i); } } private boolean gameOver(int steps){ if (winner!=null){ return true; } else 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,"tuzi").start(); new Thread(race,"wugui").start(); }}
静态代理
package demo01;//静态代理模式//真实对象和代理对象都要实现同一个接口public class StaticProxy { public static void main(String[] args) { WeddingCompany weddingCompany = new WeddingCompany(new You()); weddingCompany.HappyMarry(); }}interface Marry{ void HappyMarry();}//真实角色,你去结婚class You implements Marry{ @Override public void HappyMarry() { System.out.println("Marrying"); }}//代理角色,帮助你结婚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("before"); } private void after() { System.out.println("after"); }}
lamda表达式
其实质属于函数式编程的概念
package demo01;//推导lamda表达式public class Lamda { //3.静态内部类 static class Like2 implements ILike{ @Override public void lamda() { System.out.println("lamda2"); } } //lamda1 public static void main(String[] args) { ILike like = new Like(); like.lamda(); //lamda2 like = new Like2(); like.lamda(); //4.局部内部类 class Like3 implements ILike{ @Override public void lamda() { System.out.println("lamda3"); } } like = new Like3(); like.lamda(); //5.匿名内部类,没有类的名称,必须借助接口或者父类 like = new ILike() { @Override public void lamda() { System.out.println("lamda4"); } }; like.lamda(); //6.用lamda简化 like = ()->{ System.out.println("lamda5"); }; like.lamda(); }}//1.定义一个函数接口interface ILike{ void lamda();}//2.实现类class Like implements ILike{ @Override public void lamda() { System.out.println("lamda1"); }}
练习
package demo01;public class Lamda02 { public static void main(String[] args) { ILove love =(int a)->{ System.out.println("i love you--->"+a); }; love.love(520); //简化版本 love = a->System.out.println("i love you--->"+a); love.love(521); //只有一行代码的情况下,才可简化花括号 //前提是接口必须是函数接口 }}interface ILove{ void love(int a);}
线程停止
package demo01;import java.sql.SQLOutput;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("stop"); } } }}
线程休眠—sleep
模拟网络延时:放大问题的发生性
package demo01;//模拟倒计时public class TestSleep { public static void main(String[] args) { try { tenDown(); } catch (InterruptedException e) { e.printStackTrace(); } } public static void tenDown() throws InterruptedException { int num = 10; while(true){ Thread.sleep(1000); System.out.println(num--); if (num<=0){ break; } } }}
package demo01;import java.text.SimpleDateFormat;import java.util.Date;public class TestSleep { public static void main(String[] args) { //打印当前系统时间 Date startTime = new Date(System.currentTimeMillis());//获取当前系统时间 while(true){ try { Thread.sleep(1000); System.out.println(new SimpleDateFormat("HH:mm:ss").format(startTime)); startTime = new Date(System.currentTimeMillis()); } catch (InterruptedException e) { e.printStackTrace(); } } } //模拟倒计时 public static void tenDown() throws InterruptedException { int num = 10; while(true){ Thread.sleep(1000); System.out.println(num--); if (num<=0){ break; } } }}
线程礼让—yield
package demo01;//测试礼让线程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()+"---Start"); Thread.yield(); System.out.println(Thread.currentThread().getName()+"---Stop"); }}
线程强制执行—join
package demo01;public class TestJoin implements Runnable{ @Override public void run() { for (int i = 0; i < 1000; i++) { System.out.println("VIPPPPPPPPPPPPP"+i); } } public static void main(String[] args) throws InterruptedException { //启动我们的线程 TestJoin testJoin = new TestJoin(); Thread thread = new Thread(testJoin); thread.start(); //主线程 for (int i = 0; i < 500; i++) { if (i==200){ thread.join(); } System.out.println("main"+i); } }}
观察线程状态
package demo01;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(1000); state = thread.getState();//更新线程状态 System.out.println(state); } }}
线程优先级
package demo01;import java.sql.SQLOutput;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(4); t2.start(); t3.setPriority(2); t3.start(); t4.setPriority(1); t4.start(); t5.setPriority(10); t5.start(); }}class MyPriority implements Runnable{ @Override public void run() { System.out.println(Thread.currentThread().getName()+"--->"+Thread.currentThread().getPriority()); } }
线程同步安全
三大不安全案例
package syn;//不安全的买票//线程不安全,有负数public class UnsafeBuyTicket { public static void main(String[] args) { BuyTicket station = new BuyTicket(); new Thread(station,"A").start(); new Thread(station,"B").start(); new Thread(station,"C").start(); }}class BuyTicket implements Runnable{ int ticketNums = 10; boolean flag = true; @Override public void run() { while(flag){ buy(); } } private void buy(){ if (ticketNums<=0){ flag = false; return; } System.out.println(Thread.currentThread().getName()+" Buy the "+ticketNums--); }}
package syn;//不安全的取钱public class UnsafeBank { public static void main(String[] args) { Account account = new Account(100,"jijin"); Drawing you = new Drawing(account,50,"you"); 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()+" No Money ,Can't"); return; } //sleep可放大问题的发生性 try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } account.money = account.money-drawingMoney; nowMoney = nowMoney+drawingMoney; System.out.println(account.name+" has "+account.money); System.out.println(this.getName()+" Now Money "+nowMoney); }}
package syn;import java.util.ArrayList;import java.util.List;public class UnsafeList { 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(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(list.size()); }}
同步方法及同步块
同步方法//private synchronized void buy() throws InterruptedException {}同步块//synchronized (account){}同步块 /* new Thread(()->{synchronized (list){list.add(Thread.currentThread().getName());}}).start();}*/
CopyOnWriteArrayList
package syn;import java.util.concurrent.CopyOnWriteArrayList;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(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(list.size()); }}
死锁
多个线程互相抱着对方的资源,然后形成僵持。
package syn;public class DeadLock { public static void main(String[] args) { MakeUp g1 = new MakeUp(0,"HGN"); MakeUp g2 = new MakeUp(1,"BXGZ"); g1.start(); g2.start(); }}class Lipstick{}class Mirror{}class MakeUp extends Thread{ static Lipstick lipstick =new Lipstick(); static Mirror mirror =new Mirror(); int choice; String girlsName; MakeUp(int choice,String girlsName){ this.choice = choice; this.girlsName = girlsName; } @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.girlsName+" get lipstick"); Thread.sleep(1000); synchronized (mirror){ System.out.println(this.girlsName+" get mirror"); } } } else{ synchronized (mirror){ System.out.println(this.girlsName+" get mirror"); Thread.sleep(2000); synchronized (lipstick) { System.out.println(this.girlsName + " get lipstick"); } } } }}/* 化妆,互相持有对方的锁private void makeup() throws InterruptedException { if (choice==0){ synchronized (lipstick){ System.out.println(this.girlsName+" get lipstick"); Thread.sleep(1000); } synchronized (mirror){ System.out.println(this.girlsName+" get mirror"); } } else{ synchronized (mirror){ System.out.println(this.girlsName+" get mirror"); Thread.sleep(2000); } synchronized (lipstick) { System.out.println(this.girlsName + " get lipstick"); } } }*/
Lock锁
//定义lock锁private final ReentrantLock lock = new ReentrantLock();try { lock.lock(); //加锁 } finally { lock.unlock(); //解锁 }
package syn;import java.util.concurrent.locks.ReentrantLock;//不安全的买票//线程不安全,有负数public class UnsafeBuyTicket { public static void main(String[] args) { BuyTicket station = new BuyTicket(); new Thread(station,"A").start(); new Thread(station,"B").start(); new Thread(station,"C").start(); }}class BuyTicket implements Runnable{ //定义lock锁 private final ReentrantLock lock = new ReentrantLock(); int ticketNums = 10; boolean flag = true; @Override public void run() { while(flag){ try { lock.lock();//加锁 try { buy(); } catch (InterruptedException e) { e.printStackTrace(); } } finally { lock.unlock();//解锁 } } } private void buy() throws InterruptedException { if (ticketNums<=0){ flag = false; return; } Thread.sleep(1000); System.out.println(Thread.currentThread().getName()+" Buy the "+ticketNums--); }}
生产者消费者问题
管程法
package syn;//测试:生产者消费者模型--->利用缓冲区解决(管程法)//生产者,消费者,产品,缓冲区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(" push "+i+" chicken "); 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(" pop "+container.pop().id+" chicken "); } }}//产品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){ //如果产品满了 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; }}
信号灯法
package syn;//测试:生产者消费者问题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; 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("Movie"); }else { this.tv.play("Music"); } } }}//消费者-->观众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{ //演员表演,观众等待 //观众观看,演员等待 String voice;//表演的节目 boolean flag = true; //表演 public synchronized void play(String voice){ if (!flag){ try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(" biaoyanle "+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(" guankanle "+voice); //通知演员表演 this.notifyAll(); this.flag = !this.flag; }}
线程池
package syn;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); //2.执行 service.execute(new NyThread()); service.execute(new NyThread()); service.execute(new NyThread()); service.execute(new NyThread()); service.execute(new NyThread()); //3.关闭连接 service.shutdown(); }}class NyThread implements Runnable{ @Override public void run() { System.out.println(Thread.currentThread().getName()); }}