且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

操作线程的方法:休眠、挂起、中断

更新时间:2022-04-12 15:59:32

1.线程的休眠例子:实现在窗体中自动画线段的功能

实现界面:

操作线程的方法:休眠、挂起、中断

详细代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.lixiyu;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class SleepMethodTest extends JFrame{
/**
     *
     */
    private static final long serialVersionUID = -8195789572929250861L;
private Thread t;
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(){
    t=new Thread(new Runnable(){
        int x=30;
        int y=50;
        public void run(){
            while(true){
                try{
                    Thread.sleep(100);
                }catch(InterruptedException e){
                    e.printStackTrace();
                }
                Graphics graphics=getGraphics();//创建Graphics对象
                graphics.setColor(getC());//设置颜色
                graphics.drawLine(x, y, 150, y++);//实现在窗体中画横线,100指画出来的线的长度
                if(y>=80){
                    y=50;
                }
            }
        }
    });
    t.start();
}
public static void main(String[] args){
    init(new SleepMethodTest(),200,200);
}
public static void init(JFrame frame,int width,int height){
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setSize(width,height);
    frame.setVisible(true);
}
}


2.线程的挂起例子:通过join方法实现对进度条的控制

实现界面:

操作线程的方法:休眠、挂起、中断操作线程的方法:休眠、挂起、中断

详细代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.lixiyu;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
public class JoinTest extends JFrame{
/**
     *
     */
    private static final long serialVersionUID = 5631108658842378793L;
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(),200,150);
}
public JoinTest(){
    super();
    getContentPane().add(progressBar,BorderLayout.NORTH);
    getContentPane().add(progressBar2,BorderLayout.SOUTH);
    progressBar.setStringPainted(true);
    progressBar2.setStringPainted(true);
    threadA=new Thread(new Runnable(){//使用匿名内部类形式初始化Thread实例
        int count=0;
        @SuppressWarnings("static-access")
        public void run(){//重写run()方法
            while(true){
            progressBar.setValue(++count);//设置进度条的当前值
        try{
            threadA.sleep(100);
            threadB.join();//使线程B调用join方法
        }catch(Exception e){
            e.printStackTrace();
        }
            }
    }
    });
    threadA.start();
                      
    threadB=new Thread(new Runnable(){
        int count=0;
        @SuppressWarnings("static-access")
        public void run(){
            while(true){
                progressBar2.setValue(++count);
                try{
                    threadB.sleep(100);
                }catch(Exception e){
                    e.printStackTrace();
                }
                if(count==100)
                    break;
            }
        }
    });
    threadB.start();
    }
public static void init(JFrame frame,int width,int height){
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(width,height);
    frame.setVisible(true);
}
}


3.线程的中断例子:interrupte()设置线程正确的停止方式

实现界面:

操作线程的方法:休眠、挂起、中断

详细代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.lixiyu;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
public class InterruptedSwing extends JFrame{
/**
     *
     */
    private static final long serialVersionUID = 2575518138753855667L;
Thread thread;
final JProgressBar progressBar=new JProgressBar();
public static void main(String[] args){
    init(new InterruptedSwing(),200,150);
}
public InterruptedSwing(){
    super();
    getContentPane().add(progressBar,BorderLayout.NORTH);
    progressBar.setStringPainted(true);
             
    thread=new Thread(new Runnable(){
        int count=0;
        @SuppressWarnings("static-access")
        public void run(){
            progressBar.setValue(++count);
            try{
                thread.sleep(2000);
            }catch(InterruptedException e){
                System.out.println("当前线程中断");
            }
        }
    });
    thread.start();
    thread.interrupt();
}
public static void init(JFrame frame,int width,int height){
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(width, height);
    frame.setVisible(true);
}
}


本文转自lixiyu 51CTO博客,原文链接:http://blog.51cto.com/lixiyu/1312815,如需转载请自行联系原作者