多线程3

tech2025-07-10  3

多线程3

通过继承Thread类所创建的线程不能实现资源共享功能

package com.lx.test1; public class MyThread extends Thread { //定义车票【共享资源】 private int piao =10; public void run() { while(piao>0) { //我们通过线程的暂停来模拟 //收钱-->打票-->找钱 try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"--卖出1张票,还剩"+(--piao)+"张"); } } } package com.lx.test1; public class TestMain1 { public static void main(String[] args) { MyThread th1 = new MyThread(); MyThread th2 = new MyThread(); MyThread th3 = new MyThread(); th1.setName("窗口1"); th2.setName("窗口2"); th3.setName("窗口3"); th1.start(); th2.start(); th3.start(); } }

通过实现Runnable接口所创建的线程可以实现资源共享功能。

package com.lx.test2; public class MyThread implements Runnable { //定义车票【共享资源】 private int piao =10; public void run() { while(piao>0) { //我们通过线程的暂停来模拟 //收钱-->打票-->找钱 try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"--卖出1张票,还剩"+(--piao)+"张"); } } } package com.lx.test2; public class TestMain2 { public static void main(String[] args) { //创建目标对象 MyThread mth = new MyThread(); //创建线程对象 Thread th1 = new Thread(mth); Thread th2 = new Thread(mth); Thread th3 = new Thread(mth); //创建线程名称 th1.setName("窗口1"); th2.setName("窗口2"); th3.setName("窗口3"); //开启线程 th1.start(); th2.start(); th3.start(); } }

通过上面的实现Runnable接口的买票程序可以实现资源共享,但是卖出会卖出剩下负数的情况。 分析:当窗口3开始卖最后一张票的时候,窗口3判断还有一张票,这时窗口3开始收钱打票,当窗口3开始收钱打票的时候,线程就切换给了窗口1,由于窗口3还有来得及对票数减1,因此窗口1判断还有一张票,这时窗口1开始收钱打票,当窗口1开始收钱打票的时候,线程就切换给了窗口2,由于窗口1还有来得及对票数减1,因此窗口2判断还有一张票,这时窗口2开始收钱打票,线程切换给了窗口3,所以窗口3输出“窗口3卖出1张票,还剩0张”,输出结束以后线程就切换给窗口1,由于窗口3已经对票数减1,所以窗口1输出剩余票数的时候在窗口3减1以后的基础上再一次减1,就得到剩余-1张票,所以窗口1输出“窗口1卖出1张票,还剩-1张”,输出结束以后线程就切换给窗口2,由于窗口1已经对票数减1,所以窗口2输出剩余票数的时候在窗口1减1以后的基础上再一次减1,就得到剩余-2张票,所以窗口2输出“窗口2卖出1张票,还剩-2张”. 经过上面运行程序的分析,我得到的结果是: 当多条线程,同时访问同一个资源的时候,会产生数据不一致的错误情况。 为了解决这种数据不一致的错误情况,我们才学习线程同步。 10.5.为什么需要线程同步/线程安全? 因为当多条线程,同时访问同一个资源的时候,会产生数据不一致的错误情况。为了解决这种数据不一致的错误情况,我们才学习线程同步。 10.6.什么是线程同步/线程安全? 线程同步:当多条线程,同时访问同一个资源的时候,每一次只能由多条线程中的其中一条访问公共资源,当这一条线程访问公共资源的时候,其他的线程都处于等待状态,不能访问公共资源,当这一条线程访问完了公共资源以后,其他线程中的一条线程才能访问资源,剩下的线程继续等待,等待当前线程访问结束,实现这个过程就是线程同步也叫线程安全。 10.7.线程同步/线程安全的实现方式有几种,分别是什么,有什么区别? 有3中方式可以实现线程同步/线程安全 1.同步代码块 格式:

synchronized(同步对象){ } package com.lx.test3; public class MyThread implements Runnable { //定义车票【共享资源】 private int piao =50; public void run() { //同步代码块 synchronized(this) { while(piao>0) { //我们通过线程的暂停来模拟 //收钱-->打票-->找钱 try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"--卖出1张票,还剩"+(--piao)+"张"); } } } } package com.lx.test3; public class TestMain3 { public static void main(String[] args) { //创建目标对象 MyThread mth = new MyThread(); //创建线程对象 Thread th1 = new Thread(mth); Thread th2 = new Thread(mth); Thread th3 = new Thread(mth); Thread th4 = new Thread(mth); Thread th5 = new Thread(mth); //创建线程名称 th1.setName("窗口1"); th2.setName("窗口2"); th3.setName("窗口3"); th4.setName("窗口4"); th5.setName("窗口5"); //开启线程 th1.start(); th2.start(); th3.start(); th4.start(); th5.start(); } }

同步代码块虽然可以实现买票的效果,但是它在使用的时候,需要设置一个同步对象,由于我们很多时候都不知道这个同步对象应该是谁,容易写错,造成死锁的情况。正是应为这个缺点,我们很少使用同步代码块来实现线程同步。 2.同步方法 同步方法也是方法,所以它一定是符合方法的定义格式的。 方法的定义格式:

访问限制修饰符 方法返回值类型 方法名称(){ } 同步方法的定义格式: 访问限制修饰符 synchronized 方法返回值类型 方法名称(){ } package com.lx.test4; public class MyThread implements Runnable { //定义车票【共享资源】 private int dishu =50; private boolean flag = true; public void run() { while(flag) { sellPiao(); } } /** * 买票的同步方法 */ public synchronized void sellPiao() { if(dishu>0) { try { Thread.sleep(200); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+ "--出现地鼠,请奋力敲打他们"); }else { flag=false; } } } package com.lx.test4; public class TestMain4 { public static void main(String[] args) { //创建目标对象 MyThread mth = new MyThread(); //创建线程对象 Thread th1 = new Thread(mth); Thread th2 = new Thread(mth); Thread th3 = new Thread(mth); Thread th4 = new Thread(mth); Thread th5 = new Thread(mth); Thread th6 = new Thread(mth); Thread th7 = new Thread(mth); Thread th8 = new Thread(mth); Thread th9 = new Thread(mth); //创建线程名称 th1.setName("1号坑"); th2.setName("2号坑"); th3.setName("3号坑"); th4.setName("4号坑"); th5.setName("5号坑"); th6.setName("6号坑"); th7.setName("7号坑"); th8.setName("8号坑"); th9.setName("9号坑"); //开启线程 th1.start(); th2.start(); th3.start(); th4.start(); th5.start(); th6.start(); th7.start(); th8.start(); th9.start(); } }

3.通过Lock接口 public interface Lock Lock实现提供比使用synchronized方法和语句可以获得的更广泛的锁定操作 常用的接口方法 由于上面的锁方法是Lock接口,我们要使用就得先创建出Lock接口对象,由于Lock是个接口不能new ,我们就得使用它的子类来创建对象。 Lock接口得子类ReentrantLock ReentrantLock() 创建一个 ReentrantLock的实例。 实例:

package com.lx.test5; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class MyThread implements Runnable{ //定义车票【共享资源】 private int piao=20; //创建Lock对象 private Lock lock=new ReentrantLock(); @Override public void run() { lock.lock(); while(piao>0) { //我们通过线程的暂停来模拟 //收钱-->打票-->找钱 try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+ "--卖出1张票,还剩"+(--piao)+"张"); } lock.unlock(); } } package com.lx.test5; public class TestMain5 { public static void main(String[] args) { //创建目标对象 MyThread mth = new MyThread(); //创建线程对象 Thread th1 = new Thread(mth); Thread th2 = new Thread(mth); Thread th3 = new Thread(mth); Thread th4 = new Thread(mth); Thread th5 = new Thread(mth); //创建线程名称 th1.setName("窗口1"); th2.setName("窗口2"); th3.setName("窗口3"); th4.setName("窗口4"); th5.setName("窗口5"); //开启线程 th1.start(); th2.start(); th3.start(); th4.start(); th5.start(); } }

最新回复(0)