JAVA 多线程的简单运用

先来介绍一些基本概念。

    程序:存储在磁盘上的一些指令。
    进程:在内存中,程序运行起来后在操作系统中形成的一个进程。进程在运行的时候会向操作系统提供请求,请求硬件资源。进程会管理所有的
    线程:是进程中的一个独立的运行单位;一个进程中可以有一个线程,也可以有多个线程。线程在CPU运行,但是它需要内存资源、寄存器资源做支撑。线程没有独立的存储空间。

JAVA实现多线程的方式:
1.继承Thread类
Thread是Runnable接口的子类。
start()是线程的启动方法。
run()是线程的执行方法。
sleep(long time)是线程的休眠方法。
2.实现Runnable接口
run()是线程的执行方法,但是是抽象的。

利用多线程实现抽奖和图片的随机选取。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Image;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Prize extends JFrame{

    public static void main(String[] args) {
        Prize pe = new Prize();
        pe.showFrame();
    }

    public void showFrame(){
        this.setTitle("抽奖");
        this.setSize(500, 500);
        this.setDefaultCloseOperation(3);
        this.setLocationRelativeTo(null);
        this.setLayout(new FlowLayout());

        JLabel label = new JLabel("中奖号码:");
        //设置字体
        label.setFont(new Font("宋体",Font.BOLD,30));
        label.setPreferredSize(new Dimension(200,100));
        this.add(label);

        JButton button = new JButton("开始抽奖");
        button.setPreferredSize(new Dimension(100,30));
        this.add(button);

        JButton bu = new JButton("生成随机图片");
        bu.setPreferredSize(new Dimension(180,30));
        this.add(bu);

        //用数组存储图片地址
        String[] imgSrcs = {"C:\\Users\\庞志贤\\Desktop\\文档\\图片\\随机图片\\1.jpg",
                "C:\\Users\\某某某\\Desktop\\文档\\图片\\随机图片\\2.jpg",
                "C:\\Users\\某某某\\Desktop\\文档\\图片\\随机图片\\3.jpg",
                "C:\\Users\\某某某\\Desktop\\文档\\图片\\随机图片\\4.jpg",
                "C:\\Users\\某某某\\Desktop\\文档\\图片\\随机图片\\5.jpg",
                "C:\\Users\\某某某\\Desktop\\文档\\图片\\随机图片\\6.jpg",
                "C:\\Users\\某某某\\Desktop\\文档\\图片\\随机图片\\7.jpg",
                "C:\\Users\\某某某\\Desktop\\文档\\图片\\随机图片\\8.jpg",
                "C:\\Users\\某某某\\Desktop\\文档\\图片\\随机图片\\9.jpg",
                "C:\\Users\\某某某\\Desktop\\文档\\图片\\随机图片\\10.jpg",
                "C:\\Users\\某某某\\Desktop\\文档\\图片\\随机图片\\11.jpg",
                "C:\\Users\\某某某\\Desktop\\文档\\图片\\随机图片\\12.jpg"};
        JLabel lb = new JLabel();   
        lb.setOpaque(true);
        //设置JLabel的背景色
        lb.setBackground(Color.LIGHT_GRAY);
        lb.setPreferredSize(new Dimension(300,300));
        this.add(lb);

        this.setVisible(true);

        //将label、lb、imgSrcs传递过去
        PrizeListener pl = new PrizeListener(label,lb,imgSrcs);
        button.addActionListener(pl);
        bu.addActionListener(pl);

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

import javax.swing.JButton;
import javax.swing.JLabel;

public class PrizeListener implements ActionListener{
    private JLabel label;
    private JLabel lb;
    private String[] imgSrcs;
    private RandomListener rl;

    //构造方法
    public PrizeListener(JLabel label,JLabel lb,String[] imgSrcs) {
        this.label = label; 
        this.lb = lb;
        this.imgSrcs = imgSrcs;

        //将label、lb、imgSrcs传递过去
        rl = new RandomListener(label,lb,imgSrcs);
        //启动线程
        rl.start();
    }

    public void actionPerformed(ActionEvent e) {
        JButton button = (JButton) e.getSource();
        String text = button.getText();
        if(text.equals("开始抽奖")) {
            rl.bl = true;
            button.setText("暂停抽奖");

        }else if(text.equals("暂停抽奖")){
            button.setText("开始抽奖");
            rl.bl = false;
        }else if(text.equals("生成随机图片")) {
            rl.cl = true;
            button.setText("获取随机图片");
        }else if(text.equals("获取随机图片")) {
            rl.cl = false;
            button.setText("生成随机图片");
        }

    }

}
import java.awt.Image;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JLabel;

public class RandomListener extends Thread{
    private JLabel label;
    private JLabel lb;
    private String[] imgSrcs;
    public volatile boolean bl = false;
    public volatile boolean cl = false;

    //构造方法
    public RandomListener(JLabel label,JLabel lb,String[] imgSrcs) {
        this.label = label;
        this.lb = lb;
        this.imgSrcs = imgSrcs;

    }

    public void run() {
        while(true) {
            if(bl == true) {
                Random rand = new Random();
                int value = rand.nextInt(100) + 1;
                label.setText("中奖号码:"+value);
                try {
                    Thread.sleep(100);
                }catch(InterruptedException ie) {
                }
            }else if(cl == true) {
                Random r = new Random();
                //数组下标是从0开始,不用像上面一样+1
                //获取图片的随机地址
                String imageSrc = imgSrcs[r.nextInt(12)];
                //实例化ImageIcon
                ImageIcon image = new ImageIcon(imageSrc);
                //在JLabel中加载显示图片,图片调整到JLabel的大小
                image.setImage(image.getImage().getScaledInstance(300, 300, Image.SCALE_DEFAULT));
                lb.setIcon(image);
                try {
                    Thread.sleep(100);
                }catch(InterruptedException ie) {
                }
            }


        }



    }

}

猜你喜欢

转载自blog.csdn.net/qq1925346284/article/details/82632372
今日推荐