第二十章多线程

20.2.1继承Thread类

public class ThreadTest extends Thread {
    public void run() {
        for (int i = 1; i <= 10; i++) {
            System.out.print(i + " ");
        }
    }

    public static void main(String[] args) {
        ThreadTest t = new ThreadTest();
        t.start();
    }
}

20.2.2实现Runnable接口

import java.awt.Container;
import javax.swing.*;

public class SwingAndThread extends JFrame {
    int count = 0; // 图标横坐标

    public SwingAndThread() {
        setBounds(300, 200, 250, 100); // 绝对定位窗体大小与位置
        Container container = getContentPane();// 主容器
        container.setLayout(null); // 使窗体不使用任何布局管理器

        Icon icon = new ImageIcon("src/1.gif"); // 图标对象
        JLabel jl = new JLabel(icon);// 显示图标的标签
        jl.setBounds(10, 10, 200, 50); // 设置标签的位置与大小
        Thread t = new Thread() { // 定义匿名线程对象
            public void run() {
                while (true) {
                    jl.setBounds(count, 10, 200, 50); // 将标签的横坐标用变量表示
                    try {
                        Thread.sleep(500); // 使线程休眠500毫秒
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    count += 4; // 使横坐标每次增加4
                    if (count >= 200) {
                        // 当图标到达标签的最右边时,使其回到标签最左边
                        count = 10;
                    }
                }
            }
        };
        t.start(); // 启动线程
        container.add(jl); // 将标签添加到容器中
        setVisible(true); // 使窗体可见
        // 设置窗体的关闭方式
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new SwingAndThread(); // 实例化一个SwingAndThread对象
    }
}

20.4.1线程的休眠


import java.awt.*;
import java.util.Random;
import javax.swing.*;

public class SleepMethodTest extends JFrame {
    private static Color[] color = { Color.BLACK, Color.BLUE, Color.CYAN, Color.GREEN,  Color.ORANGE, Color.YELLOW,
            Color.RED, Color.PINK, Color.LIGHT_GRAY }; // 定义颜色数组
    private static final Random rand = new Random(); // 创建随机对象

    private static Color getC() { // 获取随机颜色值的方法
        return color[rand.nextInt(color.length)];
    }

    public SleepMethodTest() {
        Thread t = new Thread(new Runnable() { // 创建匿名线程对象
            int x = 30; // 定义初始坐标
            int y = 50;

            public void run() { 
                while (true) { // 无限循环
                    try {
                        Thread.sleep(100); // 线程休眠0.1秒
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Graphics graphics = getGraphics(); // 获取组件绘图上下文对象
                    graphics.setColor(getC()); // 设置绘图颜色
                    graphics.drawLine(x, y, 100, y++); // 绘制直线并递增垂直坐标
                    if (y >= 80) {
                        y = 50;
                    }
                }
            }
        });
        t.start(); // 启动线程
    }

    public static void main(String[] args) {
        init(new SleepMethodTest(), 100, 100);
    }

    public static void init(JFrame frame, int width, int height) { // 初始化程序界面的方法
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(width, height);
        frame.setVisible(true);
    }
}
20.4.2线程的加入


import java.awt.BorderLayout;
import javax.swing.*;

public class JoinTest extends JFrame {
	private Thread threadA; // 定义两个线程
	private Thread threadB;
	private JProgressBar progressBar = new JProgressBar(); // 定义两个进度条组件
	private JProgressBar progressBar2 = new JProgressBar();

	public static void main(String[] args) {
		JoinTest test = new JoinTest();
		test.setVisible(true);
	}

	public JoinTest() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(200, 200, 200, 100);
		getContentPane().add(progressBar, BorderLayout.NORTH); // 将进度条设置在窗体最北面
		getContentPane().add(progressBar2, BorderLayout.SOUTH); // 将进度条设置在窗体最南面
		progressBar.setStringPainted(true); // 设置进度条显示数字字符
		progressBar2.setStringPainted(true);
		// 使用匿名内部类形式初始化Thread实例
		threadA = new Thread(new Runnable() {
			int count = 0;

			public void run() { // 重写run()方法
				while (true) {
					progressBar.setValue(++count); // 设置进度条的当前值
					try {
						Thread.sleep(100); // 使线程A休眠100毫秒
						threadB.join(); // 使线程B调用join()方法
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		});
		threadA.start(); // 启动线程A
		threadB = new Thread(new Runnable() {
			int count = 0;

			public void run() {
				while (true) {
					progressBar2.setValue(++count); // 设置进度条的当前值
					try {
						Thread.sleep(100); // 使线程B休眠100毫秒
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					if (count == 100) // 当count变量增长为100时
						break; // 跳出循环
				}
			}
		});
		threadB.start(); // 启动线程B
	}
}

20.4.3线程的中断

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;

public class InterruptedSwing extends JFrame {

	public static void main(String[] args) {
		init(new InterruptedSwing(), 100, 100);
	}

	public InterruptedSwing() {
		JProgressBar progressBar = new JProgressBar(); // 创建进度条
		getContentPane().add(progressBar, BorderLayout.NORTH); // 将进度条放置在窗体合适位置
		JButton button = new JButton("停止");
		getContentPane().add(button, BorderLayout.SOUTH);
		progressBar.setStringPainted(true); // 设置进度条上显示数字
		Thread t = new Thread(new Runnable() {
			int count = 0;

			public void run() {
				while (true) {
					progressBar.setValue(++count); // 设置进度条的当前值
					try {
						Thread.sleep(100); // 使线程休眠100毫秒
					} catch (InterruptedException e) { // 捕捉InterruptedException异常
						System.out.println("当前线程序被中断");
						break;
					}
				}
			}
		});

		button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				t.interrupt(); // 中断线程
			}
		});
		t.start(); // 启动线程
	}

	public static void init(JFrame frame, int width, int height) {
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(width, height);
		frame.setVisible(true);
	}
}

20.5线程的优先级


public class PriorityTest implements Runnable {
	String name;

	public PriorityTest(String name) {
		this.name = name;
	}

	@Override
	public void run() {
		String tmp = "";
		for (int i = 0; i < 50000; i++) {// 完成五万次字符串拼接
			tmp += i;
		}
		System.out.println(name + "线程完成任务");
	}

	public static void main(String[] args) {
		Thread a = new Thread(new PriorityTest("A"));
		a.setPriority(1);// A线程优先级最小
		Thread b = new Thread(new PriorityTest("B"));
		b.setPriority(3);
		Thread c = new Thread(new PriorityTest("C"));
		c.setPriority(7);
		Thread d = new Thread(new PriorityTest("D"));
		d.setPriority(10);// D线程优先级最大
		a.start();
		b.start();
		c.start();
		d.start();
	}
}

20.6.1线程同步机制

同步方法就是在方法前面用synchronized 关键字修饰的方法,其语法如下:

synchronized void f(){}

public class SynchronizedTest implements Runnable {
    int num = 10; // 设置当前总票数

    public void run() {
        while (true) { // 设置无限循环
            synchronized (this) { // 设置同步代码块
                if (num > 0) { // 判断当前票数是否大于0
                    try {
                        Thread.sleep(100); // 使当前线程休眠100毫秒
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    // 票数减1
                    System.out.println(Thread.currentThread().getName() + "——票数" + num--);
                }
            }
        }
    }

    public static void main(String[] args) {
        // 实例化类对象
        SynchronizedTest t = new SynchronizedTest();
        // 以该类对象分别实例化4个线程
        Thread tA = new Thread(t, "线程一");
        Thread tB = new Thread(t, "线程二");
        Thread tC = new Thread(t, "线程三");
        Thread tD = new Thread(t, "线程四");
        tA.start(); // 分别启动线程
        tB.start();
        tC.start();
        tD.start();
    }
}

猜你喜欢

转载自blog.csdn.net/2302_76534913/article/details/134681339
今日推荐