您当前的位置: 首页 > 

壹小俊

暂无认证

  • 2浏览

    0关注

    885博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

多线程的学习

壹小俊 发布时间:2022-01-29 20:23:48 ,浏览量:2

public class ThreadTest extends Thread {
    private int count = 10;
    @Override
    public void run() {
        super.run();
        while (true){
            System.out.println(count);
            if(--count == 0){
                return;
            }
        }
    }

    public static void main(String[] args) {
        new ThreadTest().start();
    }
}
import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class SwingAndThread extends JFrame {
    private JLabel jl = new JLabel();
    private static Thread t;
    private int count = 0;
    private Container container = getContentPane();

    public SwingAndThread(){
        setBounds(300,200,250,100);
        container.setLayout(null);
        URL url = SwingAndThread.class.getResource("./WechatIMG2.png");
        Icon icon = new ImageIcon(url);
        jl.setIcon(icon);
        jl.setHorizontalAlignment(SwingConstants.LEFT);
        jl.setBounds(10,10,200,50);
        jl.setOpaque(true);
        t = new Thread(new Runnable() {
            @Override
            public void run() {
                while (count=80){
                        y=50;
                    }
                }

            }
        });
        t.start();
    }

    public static void main(String[] args) {
        init(new SleepMethodTest(),100,100);
    }
    public static void init(JFrame jf,int width,int height){
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setSize(width,height);
        jf.setVisible(true);
    }
}
public class ThreadSafeTest implements Runnable {
    int num = 10;

    @Override
    public void run() {
        while (true){
            if(num>0){
                try {
                    Thread.sleep(100);
                }catch (Exception e){
                    e.printStackTrace();
                }
                System.out.println("tickets"+num--);
            }
        }
    }

    public static void main(String[] args) {
        ThreadSafeTest t = new ThreadSafeTest();
        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();
    }
}
public class ThreadSafeTest2 implements Runnable {
    int num = 10;

    @Override
    public void run() {
        while (true){
            synchronized (""){
                if(num>0){
                    try {
                        Thread.sleep(100);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    System.out.println("tickets"+num--);
                }
            }
        }
    }

    public static void main(String[] args) {
        ThreadSafeTest2 t = new ThreadSafeTest2();
        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();
    }
}

关注
打赏
1664335782
查看更多评论
立即登录/注册

微信扫码登录

0.0437s