Java多线程02

Posted xingweikun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
	}

}

在这里插入图片描述

线程的加入

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

}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

线程的中断

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的主要内容,如果未能解决你的问题,请参考以下文章

Java多线程与并发库高级应用-工具类介绍

Java多线程与并发库高级应用-工具类介绍

多线程 Thread 线程同步 synchronized

20160226.CCPP体系详解(0036天)

Java多线程具体解释

Java多线程系列--“JUC集合”02之 CopyOnWriteArrayList