• 第二十章:多线程


    进程

    线程的特点

    1.进程是资源分配的最小单位,线程是最小的执行单位

    2.一个进程可以有多个线程

    3.线程共享进程资源 

    package twentyth;
     
     
    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.1

    线程的实现

    package twentyth;
     
    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对象
        }
    }

    package twentyth;
     
     
    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.3


     

  • 相关阅读:
    TabLayout+Fragment+ViewPager实现Tab页面效果
    计算机毕业设计微信小程序校园辩论管理平台
    【C语言练习——交换两个变量的值】
    每日word+excel+ppt----雷打不动,好吗!!
    Android 静态代码检查
    uview ui与element ui的区别和用法
    QT运行界面与画布大小不一致问题(一步到位)
    LeetCode50天刷题计划第二季(Day 31 — 两数之和 II - 输入有序数组(11.10-11.20)分数到小数(11.30-12.30)
    哲寻面试(部分)(未完全解析)
    LVGL---滚轮(lv_roller)
  • 原文地址:https://blog.csdn.net/2301_76534800/article/details/134564273