Java多线程02

文章目录

  • 操作线程的方法
    • 线程的休眠
    • 线程的加入
    • 线程的中断

操作线程的方法

线程的休眠

package 多线程;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;

import javax.swing.*;


/*
 * 线程的休眠
 * */

public class SleepMethodTest extends JFrame {
     
	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;

			@Override
			public void run() {
     
				while (true) {
     
					try {
     
						Thread.sleep(100);
					} 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);
	}

}

Java多线程02_第1张图片

线程的加入

package 多线程;

import java.awt.BorderLayout;

import javax.swing.*;

/*
 * 线程的加入
 * */

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 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;

			public void run() {
     
				while (true) {
     
					progressBar2.setValue(++count);
					try {
     
						Thread.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.DISPOSE_ON_CLOSE);
		frame.setSize(width, height);
		frame.setVisible(true);
	}

}

Java多线程02_第2张图片
Java多线程02_第3张图片
Java多线程02_第4张图片

线程的中断

package 多线程;

import java.awt.BorderLayout;

import javax.swing.*;

/*
 * 线程的中断
 * */

public class InterruptedSwing extends JFrame {
     
	Thread thread;

	public static void main(String[] args) {
     
		init(new InterruptedSwing(), 100, 100);

	}

	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(100);
					} catch (InterruptedException e) {
     
						System.out.println("当前线程序被中断");
						break;
					}
				}

			}
		});
		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);
	}

}

Java多线程02_第5张图片

你可能感兴趣的:(Java,多线程,thread)