• 学习JAVA第七课:多线程


    1、线程(Thread)
    • 进程(Process):操作系统中,“同时”运行的多个程序
    • 线程(Thread):一个应用程序中,“同时”运行的多个任务,例如:一边下载,一边播放;等等
    • 例如:QQ可以同时支持聊天,还能够“同时”下载文件
    • youku播放视频,“同时”还能下载视频,“同时”弹幕出现
      “同时”是怎么实现的?实际上是“时间片分轮转分时”
    2、如何实现多线程?
    • 案例:下载文件,播放文件,如果不用多线程,将会串行运行
      -实现多线程两种方法
    • 1、让线程所在的类继承java.lang.Thread;
      • 将线程实现代码写在从Thread中重写的run函数中;
      • 使用线程类的start函数,启动线程,调用run

    例子:同时下载文件和播放音乐

    class Player extends Thread{
    	public void run()
    	{
    		for(int i = 0;i <= 10;i++)
    		{
    			try {
    				Thread.sleep(1000);
    			}catch(Exception e) {}
    			System.out.println("音乐播放"+i*10+"%");
    		}
    	}
    }
    
    class DownLoader extends Thread{//②
    	public void run ()//①
    	{
    		for(int i = 0;i <= 10;i++)
    		{
    			try {
    				Thread.sleep(1000);
    			}catch(Exception e) {}
    			System.out.println("文件下载"+i*10+"%");
    		}
    	}
    }
    class Test{	
    	
    	public static void main (String[] args) {
    		Player p = new Player();
    		DownLoader d = new DownLoader();
    		p.start();//③
    		d.start();
    	}
    	
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    
    class Player extends Thread{
    	public void run() {
    		this.play();
    	}
    	public void play()
    	{
    		for(int i = 0;i <= 10;i++)
    		{
    			try {
    				Thread.sleep(1000);
    			}catch(Exception e) {}
    			System.out.println("音乐播放"+i*10+"%");
    		}
    	}
    }
    
    class DownLoader extends Thread{//②
    	public void run() {
    		this.play();
    	}
    	public void play ()//①
    	{
    		for(int i = 0;i <= 10;i++)
    		{
    			try {
    				Thread.sleep(1000);
    			}catch(Exception e) {}
    			System.out.println("文件下载"+i*10+"%");
    		}
    	}
    }
    class Test{	
    	
    	public static void main (String[] args) {
    		Player p = new Player();
    		DownLoader d = new DownLoader();
    		p.start();//③
    		d.start();
    	}
    	
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 2、让线程所在的类实现java.lang.Runnable接口;
      • 将线程实现代码写在从Runnable中重写的run函数中;
      • 实例化该类对象,将对象传入新的线程对象,使用其start函数,调用run
    
    class Downloader implements Runnable{//①
    	public void run(){  //②
    		for(int i=1;i<=10;i++){
    			try{ Thread.sleep(1000);} catch(Exception e){}
    			System.out.println("下载进度:" + i*10 + "%");
    		}
    	}
    }
    class Player implements Runnable{
    	public void run(){
    		for(int i=1;i<=10;i++){
    			try{ Thread.sleep(1000);} catch(Exception e){}
    			System.out.println("播放进度:" + i*10 + "%");
    		}
    	}
    }
    class Test{	
    	public static void main (String[] args) {
    		Downloader d = new Downloader();
    		Player p = new Player();
    		Thread t1 = new Thread(d);	//③
    		Thread t2 = new Thread(p);
    		t1.start();							
    		t2.start();
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 有何区别?如何选择?
      • 方法1,让代码直接成为线程对象;方法2,让代码可以包装进线程对象;方法1相当于线程独立运行,方法2让代码依附线程运行;
      • 方法2可以让一段代码依附多个线程,实现“一段代码的多线程化”;实际上使用时,方法2因为是实现了Runnable接口,因此同时类还可以继承其他类;
        class AAA extends XXX implements Runnable{}
    3、线程的控制
    • 包括线程开始、暂停、继续、终止
    • 线程开始:start函数
    • 线程暂停:suspend函数
    • 线程继续:resume
    • 终止: stop

    例子:播放音乐,3秒之后,暂停5秒,再次运行

    class Downloader extends Thread{//①
    	public void run(){  //②
    		for(int i=1;i<=10;i++){
    			try{ Thread.sleep(1000);} catch(Exception e){}
    			System.out.println("下载进度:" + i*10 + "%");
    		}
    	}
    }
    
    class Test{	
    	public static void main (String[] args) throws Exception{
    		Downloader d = new Downloader();
    		d.start();
    		Thread.sleep(3000);
    		d.suspend();
    		Thread.sleep(4000);
    		d.resume();
    		Thread.sleep(2000);
    		d.stop();
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    ----以上方法是不安全的,要避免使用!因为以上方法有deadlock-prone(可能造成死锁

    • suspend函数在线程暂停时,仍然持有线程占用的资源,其他线程不能访问该资源,只能等待resume之后运行完毕释放后才能访问。如果循环等待,则造成死锁
    • 如何解决?不要使用上面几个函数
      • 线程暂停,就是让线程结束;线程重新开始,就是新开启线程;线程结束的标志:run函数运行完毕
        -注意保存线程暂停之后的运行状态(保存现场)当前状态
    package customer;
    
    class Downloader extends Thread{//①
    	boolean RUN = true;
    	static int percent = 1;
    	public void run(){  //②
    		for(int i=percent;i<=10 && RUN;i++){
    			try{ Thread.sleep(1000);} catch(Exception e){}
    			System.out.println("下载进度:" + i*10 + "%");
    			percent = i+1;
    		}
    	}
    }
    
    class Test{	
    	public static void main (String[] args) throws Exception{
    		Downloader d = new Downloader();
    		d.start();
    		Thread.sleep(3000);
    		d.RUN = false;
    		Thread.sleep(4000);
    		d = new Downloader();
    		d.start();
    		Thread.sleep(2000);
    		d.RUN = false;
    
    		
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    4、线程同步

    多个线程访问同一资源。
    两个线程进行文件下载。

    class Downloader extends Thread{//①
    	boolean RUN = true;
    	int i = 0;
    	public void run(){  //②
    		while(RUN){
    			try{ Thread.sleep(1000);} catch(Exception e){}//这个很重要,不能在sy里面
    			synchronized(this){//这段代码运行时CPU不会被抢占
    				
    				i++;
    				System.out.println("文件下载:" + i*10 + "%");
    				if(i>=9) break;
    			}
    		}
    	}
    }
    
    class Test{	
    	public static void main (String[] args) throws Exception{
    		Downloader d = new Downloader();
    		Thread t1 = new Thread(d);		
    		Thread t2 = new Thread(d);	
    		t1.start();
    		t2.start();
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    synchronized:同步代码,保持原子性(代码运行中,别的线程不能抢占)

  • 相关阅读:
    c# IEnumerable--扩展方法
    JAVA面试题100道(纯手敲整理。注:面试不一定要全部答出来,只需要答出关键字即可)
    基于PCA(主成分分析法)对信用评分卡反欺诈案例 代码+数据
    基于ASP.NET Core 5.0使用RabbitMQ消息队列实现事件总线(EventBus)
    彻底颠覆无线蓝牙,华为全新黑科技「星闪」有何魅力
    一起Talk Android吧(第五百五十一回:如何自定义SplashScreen)
    yolov5训练步骤及安全帽检测
    卷积的计算过程
    Golang 切片做参数
    终端变革和6G突破:对6G终端的研究判断和发展展望
  • 原文地址:https://blog.csdn.net/weixin_62529383/article/details/127604841