第二十章 多线程

20.2创建线程
20.2.1继承Thread类
Thread类是Java.lang包中的一个类,从这个类中实例化的对象代表线程,程序员启动一个新线程需要建议Thread实例。

public class ThreadTest extedns Thread{}
run方法格式:

public void run(){}
20.1让线程循环打印1-10
 

  1. public class ThreadTest extends Thread {
  2.     public void run() {
  3.         for (int i = 1; i <= 10; i++) {
  4.             System.out.print(i + " ");
  5.         }
  6.     }
  7.  
  8.     public static void main(String[] args) {
  9.         ThreadTest t = new ThreadTest();
  10.         t.start();
  11.     }
  12. }

//20.1


20.2.2实现Runnable类

public class Tread extends Object implements Runnable


20.2让窗口的图标移动

  1. import java.awt.Container;
  2. import javax.swing.*;
  3. //20.2
  4. public class SwingAndThread extends JFrame {
  5.     int count = 0; // 图标横坐标
  6.  
  7.     public SwingAndThread() {
  8.         setBounds(300, 200, 250, 100); // 绝对定位窗体大小与位置
  9.         Container container = getContentPane();// 主容器
  10.         container.setLayout(null); // 使窗体不使用任何布局管理器
  11.  
  12.         Icon icon = new ImageIcon("src/1.gif"); // 图标对象
  13.         JLabel jl = new JLabel(icon);// 显示图标的标签
  14.         jl.setBounds(10, 10, 200, 50); // 设置标签的位置与大小
  15.         Thread t = new Thread() { // 定义匿名线程对象
  16.             public void run() {
  17.                 while (true) {
  18.                     jl.setBounds(count, 10, 200, 50); // 将标签的横坐标用变量表示
  19.                     try {
  20.                         Thread.sleep(500); // 使线程休眠500毫秒
  21.                     } catch (InterruptedException e) {
  22.                         e.printStackTrace();
  23.                     }
  24.                     count += 4; // 使横坐标每次增加4
  25.                     if (count >= 200) {
  26.                         // 当图标到达标签的最右边时,使其回到标签最左边
  27.                         count = 10;
  28.                     }
  29.                 }
  30.             }
  31.         };
  32.         t.start(); // 启动线程
  33.         container.add(jl); // 将标签添加到容器中
  34.         setVisible(true); // 使窗体可见
  35.         // 设置窗体的关闭方式
  36.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  37.     }
  38.  
  39.     public static void main(String[] args) {
  40.         new SwingAndThread(); // 实例化一个SwingAndThread对象
  41.     }
  42. }

20.3线程的生命周期


20.4操作线程的方法
20.4.1线程的休眠
sleep方法  该时间以毫秒为单位

try{
Thread.sleep(2000);
}catch(InterruptedException e){
e.printStackTrace();
}
20.3每0.1秒绘制一条随机颜色的线条
 

  1. //20.3
  2. import java.awt.*;
  3. import java.util.Random;
  4. import javax.swing.*;
  5.  
  6. public class SleepMethodTest extends JFrame {
  7.     private static Color[] color = { Color.BLACK, Color.BLUE, Color.CYAN, Color.GREEN, Color.ORANGE, Color.YELLOW,
  8.             Color.RED, Color.PINK, Color.LIGHT_GRAY }; // 定义颜色数组
  9.     private static final Random rand = new Random(); // 创建随机对象
  10.  
  11.     private static Color getC() { // 获取随机颜色值的方法
  12.         return color[rand.nextInt(color.length)];
  13.     }
  14.  
  15.     public SleepMethodTest() {
  16.         Thread t = new Thread(new Runnable() { // 创建匿名线程对象
  17.             int x = 30; // 定义初始坐标
  18.             int y = 50;
  19.  
  20.             public void run() { 
  21.                 while (true) { // 无限循环
  22.                     try {
  23.                         Thread.sleep(100); // 线程休眠0.1秒
  24.                     } catch (InterruptedException e) {
  25.                         e.printStackTrace();
  26.                     }
  27.                     Graphics graphics = getGraphics(); // 获取组件绘图上下文对象
  28.                     graphics.setColor(getC()); // 设置绘图颜色
  29.                     graphics.drawLine(x, y, 100, y++); // 绘制直线并递增垂直坐标
  30.                     if (y >= 80) {
  31.                         y = 50;
  32.                     }
  33.                 }
  34.             }
  35.         });
  36.         t.start(); // 启动线程
  37.     }
  38.  
  39.     public static void main(String[] args) {
  40.         init(new SleepMethodTest(), 100, 100);
  41.     }
  42.  
  43.     public static void init(JFrame frame, int width, int height) { // 初始化程序界面的方法
  44.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  45.         frame.setSize(width, height);
  46.         frame.setVisible(true);
  47.     }
  48. }

20.4.2线程的加入
当某个线程使用join()方法加入另一个线程时,另一个线程会等待该线程执行完毕后再继续执行。

20.4让进度条A等待进度条B

  1. //20.4
  2. import java.awt.BorderLayout;
  3. import javax.swing.*;
  4.  
  5. public class JoinTest extends JFrame {
  6.     private Thread threadA; // 定义两个线程
  7.     private Thread threadB;
  8.     private JProgressBar progressBar = new JProgressBar(); // 定义两个进度条组件
  9.     private JProgressBar progressBar2 = new JProgressBar();
  10.  
  11.     public static void main(String[] args) {
  12.         JoinTest test = new JoinTest();
  13.         test.setVisible(true);
  14.     }
  15.  
  16.     public JoinTest() {
  17.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  18.         setBounds(200, 200, 200, 100);
  19.         getContentPane().add(progressBar, BorderLayout.NORTH); // 将进度条设置在窗体最北面
  20.         getContentPane().add(progressBar2, BorderLayout.SOUTH); // 将进度条设置在窗体最南面
  21.         progressBar.setStringPainted(true); // 设置进度条显示数字字符
  22.         progressBar2.setStringPainted(true);
  23.         // 使用匿名内部类形式初始化Thread实例
  24.         threadA = new Thread(new Runnable() {
  25.             int count = 0;
  26.  
  27.             public void run() { // 重写run()方法
  28.                 while (true) {
  29.                     progressBar.setValue(++count); // 设置进度条的当前值
  30.                     try {
  31.                         Thread.sleep(100); // 使线程A休眠100毫秒
  32.                         threadB.join(); // 使线程B调用join()方法
  33.                     } catch (InterruptedException e) {
  34.                         e.printStackTrace();
  35.                     }
  36.                 }
  37.             }
  38.         });
  39.         threadA.start(); // 启动线程A
  40.         threadB = new Thread(new Runnable() {
  41.             int count = 0;
  42.  
  43.             public void run() {
  44.                 while (true) {
  45.                     progressBar2.setValue(++count); // 设置进度条的当前值
  46.                     try {
  47.                         Thread.sleep(100); // 使线程B休眠100毫秒
  48.                     } catch (InterruptedException e) {
  49.                         e.printStackTrace();
  50.                     }
  51.                     if (count == 100) // 当count变量增长为100时
  52.                         break; // 跳出循环
  53.                 }
  54.             }
  55.         });
  56.         threadB.start(); // 启动线程B
  57.     }
  58. }

20.4.3线程的中断
现在提倡run()方法中使用无限循环的形式,然后使用一个布尔型标记控制循环的停止。

20.5单机按钮停止进度条滚动

  1. import java.awt.BorderLayout;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import javax.swing.JButton;
  5. import javax.swing.JFrame;
  6. import javax.swing.JProgressBar;
  7. //20.5
  8. public class InterruptedSwing extends JFrame {
  9.  
  10.     public static void main(String[] args) {
  11.         init(new InterruptedSwing(), 100, 100);
  12.     }
  13.  
  14.     public InterruptedSwing() {
  15.         JProgressBar progressBar = new JProgressBar(); // 创建进度条
  16.         getContentPane().add(progressBar, BorderLayout.NORTH); // 将进度条放置在窗体合适位置
  17.         JButton button = new JButton("停止");
  18.         getContentPane().add(button, BorderLayout.SOUTH);
  19.         progressBar.setStringPainted(true); // 设置进度条上显示数字
  20.         Thread t = new Thread(new Runnable() {
  21.             int count = 0;
  22.  
  23.             public void run() {
  24.                 while (true) {
  25.                     progressBar.setValue(++count); // 设置进度条的当前值
  26.                     try {
  27.                         Thread.sleep(100); // 使线程休眠100毫秒
  28.                     } catch (InterruptedException e) { // 捕捉InterruptedException异常
  29.                         System.out.println("当前线程序被中断");
  30.                         break;
  31.                     }
  32.                 }
  33.             }
  34.         });
  35.  
  36.         button.addActionListener(new ActionListener() {
  37.  
  38.             @Override
  39.             public void actionPerformed(ActionEvent e) {
  40.                 t.interrupt(); // 中断线程
  41.             }
  42.         });
  43.         t.start(); // 启动线程
  44.     }
  45.  
  46.     public static void init(JFrame frame, int width, int height) {
  47.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  48.         frame.setSize(width, height);
  49.         frame.setVisible(true);
  50.     }
  51. }

20.4.4线程的礼让
yield()方法使具有同样优先级的线程有进入可执行状态的机会,在当前线程放弃执行权会再回到就绪状态。

20.5线程的优先级
每个线程都具有各自的优先级,线程的优先级可以表明在程序中该线程的重要性,如果有会让你多线程处于就绪状态,系统会根据优先级来决定首先使用哪一个线程进入运行状态。

20.6观察不同优先级的线程执行完毕顺序
 

 

  1. public class PriorityTest implements Runnable {
  2.     String name;
  3.  
  4.     public PriorityTest(String name) {
  5.         this.name = name;
  6.     }
  7.  
  8.     @Override
  9.     public void run() {
  10.         String tmp = "";
  11.         for (int i = 0; i < 50000; i++) {// 完成五万次字符串拼接
  12.             tmp += i;
  13.         }
  14.         System.out.println(name + "线程完成任务");
  15.     }
  16.  
  17.     public static void main(String[] args) {
  18.         Thread a = new Thread(new PriorityTest("A"));
  19.         a.setPriority(1);// A线程优先级最小
  20.         Thread b = new Thread(new PriorityTest("B"));
  21.         b.setPriority(3);
  22.         Thread c = new Thread(new PriorityTest("C"));
  23.         c.setPriority(7);
  24.         Thread d = new Thread(new PriorityTest("D"));
  25.         d.setPriority(10);// D线程优先级最大
  26.         
  27.         a.start();
  28.         b.start();
  29.         c.start();
  30.         d.start();
  31.     }
  32. }

20.6线程同步
在单线程程序中,每一次只能做一件事情,后面的事情需要等待前面的事情完成后才可以进行,但是如果使用多线程线程,就会发生两个线程抢占资源问题。

20.6.1线程安全
20.6.2线程同步机制
1,同步块

synchronized(Object){
}


2,同步方法

synchronized void f(){}


20.7开发线程安全的火车售票系统
 

  1. public class SynchronizedTest implements Runnable {
  2.     int num = 10; // 设置当前总票数
  3.  
  4.     public void run() {
  5.         while (true) { // 设置无限循环
  6.             synchronized (this) { // 设置同步代码块
  7.                 if (num > 0) { // 判断当前票数是否大于0
  8.                     try {
  9.                         Thread.sleep(100); // 使当前线程休眠100毫秒
  10.                     } catch (InterruptedException e) {
  11.                         e.printStackTrace();
  12.                     }
  13.                     // 票数减1
  14.                     System.out.println(Thread.currentThread().getName() + "——票数" + num--);
  15.                 }
  16.             }
  17.         }
  18.     }
  19.  
  20.     public static void main(String[] args) {
  21.         // 实例化类对象    
  22.         SynchronizedTest t = new SynchronizedTest();
  23.         // 以该类对象分别实例化4个线程
  24.         Thread tA = new Thread(t, "线程一");
  25.         Thread tB = new Thread(t, "线程二");
  26.         Thread tC = new Thread(t, "线程三");
  27.         Thread tD = new Thread(t, "线程四");
  28.         tA.start(); // 分别启动线程
  29.         tB.start();
  30.         tC.start();
  31.         tD.start();
  32.     }
  33. }


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值