多线程的学习

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<=200){
                    jl.setBounds(count,10,200,50);
                    try{
                        Thread.sleep(1000);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    count += 4;
                    if(count==200){
                        count = 10;
                    }
                }
            }
        });
        t.start();
        container.add(jl);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    }

    public static void main(String[] args) {
        new SwingAndThread();
    }
}
public class RunnableTest {
    private static int count = 10;
    private static int count2 = 15;

    public static void main(String[] args) {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                }catch (Exception e){
                    e.printStackTrace();
                }
                while (true){
                    System.out.println(count);
                    if(--count == 0){
                        return;
                    }
                }
            }
        });
        t.start();
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    System.out.println(count2);
                    if(--count2 == 0){
                        return;
                    }
                }
            }
        });
        t1.start();
    }
}
import javax.swing.*;
import java.awt.*;

public class InterruptedSwing extends JFrame {
    Thread thread;
    public static void main(String[] args) {
        init(new InterruptedSwing(),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 InterruptedSwing(){
        super();
        final JProgressBar progressBar = new JProgressBar();
        getContentPane().add(progressBar, BorderLayout.NORTH);
        progressBar.setStringPainted(true);
        thread = new Thread(new Runnable() {
            int count = 0;
            @Override
            public void run() {
                while (true){
                    progressBar.setValue(++count);
                    try{
                        thread.sleep(1000);
                    }catch (InterruptedException e){
                        System.out.println("当前线程被中断");
                        break;
                    }
                }
            }
        });
        thread.start();
        thread.interrupt();
    }
}
import javax.swing.*;
import java.awt.*;

public class JoinTest extends JFrame {
    private Thread threadA;
    private Thread threadB;
    final  JProgressBar progressBar = new JProgressBar();
    final  JProgressBar progressBar2 = new JProgressBar();
    int count = 0;

    public static void main(String[] args) {
        init(new JoinTest(),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 JoinTest(){
        super();
        getContentPane().add(progressBar, BorderLayout.NORTH);
        getContentPane().add(progressBar2,BorderLayout.SOUTH);
        progressBar.setStringPainted(true);
        progressBar2.setStringPainted(true);
        threadA = new Thread(new Runnable() {
            int count = 0;
            @Override
            public void run() {
                while (true){
                    progressBar.setValue(++count);
                    try {
                        Thread.sleep(100);
                        threadB.join();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        });
        threadA.start();
        threadB = new Thread(new Runnable() {
            int count = 0;
            @Override
            public void run() {
                while (true){
                    progressBar2.setValue(++count);
                    try{
                        Thread.sleep(100);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    if(count==100){
                        break;
                    }
                }
            }
        });
        threadB.start();
    }
}
import javax.swing.*;
import java.awt.*;

public class PriorityTest extends JFrame {
    Thread threadA;
    Thread threadB;
    Thread threadC;
    Thread threadD;
    final  JProgressBar progressBar = new JProgressBar();
    final  JProgressBar progressBar2 = new JProgressBar();
    final  JProgressBar progressBar3 = new JProgressBar();
    final  JProgressBar progressBar4 = new JProgressBar();

    public static void main(String[] args) {
        init(new PriorityTest(),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 PriorityTest(){
        super();
        getContentPane().add(progressBar, BorderLayout.NORTH);
        progressBar.setStringPainted(true);
        getContentPane().add(progressBar2, BorderLayout.SOUTH);
        progressBar2.setStringPainted(true);
        getContentPane().add(progressBar3, BorderLayout.WEST);
        progressBar3.setStringPainted(true);
        getContentPane().add(progressBar4, BorderLayout.EAST);
        progressBar4.setStringPainted(true);
        threadA = new Thread(new MyThread(progressBar));
        threadB = new Thread(new MyThread(progressBar2));
        threadC = new Thread(new MyThread(progressBar3));
        threadD = new Thread(new MyThread(progressBar4));
        setPriority("threadA",5,threadA);
        setPriority("threadB",5,threadB);
        setPriority("threadC",4,threadC);
        setPriority("threadD",3,threadD);
    }

    public static void setPriority(String threadName,int priority,Thread t){
        t.setPriority(priority);
        t.setName(threadName);
        t.start();
    }

    private final class MyThread implements Runnable {
        private final JProgressBar bar;
        int count = 0;
        private MyThread(JProgressBar bar){
            this.bar = bar;
        }
        public void run(){
            while (true){
                bar.setValue(count+=10);
                try{
                    Thread.sleep(1000);
                }catch (InterruptedException e){
                    System.out.println("当前线程被中断");
                }
            }
        }
    }
}
import javax.swing.*;
import java.awt.*;
import java.util.Random;

public class SleepMethodTest extends JFrame {
    private Thread t;
    private static Color[] colors = {
            Color.BLACK,
            Color.BLUE,
            Color.CYAN,
            Color.GREEN,
            Color.ORANGE,
            Color.YELLOW,
            Color.RED,
            Color.PINK,
            Color.LIGHT_GRAY
    };
    private static final Random random = new Random();

    private static Color getColors() {
        return colors[random.nextInt(colors.length)];
    }

    public SleepMethodTest(){
        t = new Thread(new Runnable() {
            int x = 30;
            int y = 50;
            @Override
            public void run() {
                while (true){
                    try{
                        Thread.sleep(100);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    Graphics graphics = getGraphics();
                    graphics.setColor(getColors());
                    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 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();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

韩淼燃

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值