线程就好比人可以在同一时刻完成多见事,java中描述为:将并发完成的的每一件事情称为线程。
线程的两种实现方式
继承Thread类和实现Runnable接口
public class ThreadTest extends Thread{
public void run(){}//重写run方法
}
public class ThreadTest implements Runable{
}
Runable接口的程序会创建一个Thread对象,并将Runable对象与Thread对象相关联。
Thread类中有两个构造方法
public Thread( Runable target);
public Thread(Runable target,String name);
这两个构造方法的参数中都存在Runable实列,使用这两个构造方法可以将Runable方法实列与Thread实列相关联。
使用runable接口启动新的线程的步骤如下:
(1)建立/Runable对象
(2)使用参数为Runable对象的构造方法创建Thread实列
(3)调用start方法启动线程
线程的生命周期:
七种状态的关系
操作线程的方法:
线程休眠
实列代码
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class Example 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 rand = new Random();
private static Color getC() {
return colors[rand.nextInt(colors.length)];
}
public Example() {
Init();
t = new Thread(new Runnable() {
int x = 30;
int y = 50;
@Override
public void run() {
int x = 30;
int y = 50;
while (true) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
Graphics graphics = getGraphics();
graphics.setColor(getC());
graphics.drawLine(x, y, 100, y++);
}
}
});
t.start();
}
public void Init() {
this.setVisible(true);
this.setSize(500, 500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
class main{
public static void main(String[] args) {
new Example();
}
}
线程的加入:
jion()方法
import javax.swing.*;
import java.awt.*;
public class Example extends JFrame {
private Thread A;
private Thread B;
final JProgressBar progressBar1 = new JProgressBar();//进度条组件
final JProgressBar progressBar2 = new JProgressBar();
public Example() {
Init();
getContentPane().add(progressBar1, BorderLayout.NORTH);
getContentPane().add(progressBar2, BorderLayout.SOUTH);
progressBar1.setStringPainted(true);//设置进度条显示数字字符
progressBar2.setStringPainted(true);
A = new Thread(new Runnable() {
int count = 0;
@Override
public void run() {
while (true) {
progressBar1.setValue(++count);
try {
Thread.sleep(100);
B.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
if (count == 100)
break;
}
}
});
A.start();
B = new Thread(new Runnable() {
int count = 0;
@Override
public void run() {
while (true) {
progressBar2.setValue(++count);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (count == 100)
break;
}
}
});
B.start();
}
public void Init(){
this.setSize(200,200);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
class main{
public static void main(String[] args) {
new Example();
}
}
线程的中断
interrupt()
import javax.swing.*;
import java.awt.*;
public class Example extends JFrame {
Thread thread;
public Example(){
Init();
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();
}
public void Init(){
this.setSize(200,200);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
class main{
public static void main(String[] args) {
new Example();
}
}
线程的同步机制:
java中提供了线程同步机制,可以有效的防止资源冲突,同步机制使用synchronized
例如:
public class Example implements Runnable {
int count=10;
@Override
public void run() {
while (true) {
synchronized ("") {
if (count > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("tickets" + --count);
}
}
}
}
}
class main {
public static void main(String[] args) throws InterruptedException {
Example example = new Example();
Thread A = new Thread(example);
Thread B = new Thread(example);
Thread C = new Thread(example);
Thread D = new Thread(example);
A.start();
B.start();
C.start();
D.start();
}
}
若不加关键字synchronized当线程A执行run方法时,还没来得及做递减操作,就指定它sleep()方法进入就绪状态,这是其它进程都进入了run()方法,发现count大于0,但此时A休眠时间已到,将count递减,其它线程也对count进行递减操作,导致输出结果有负数。
当把共享资源的操作放在同步方法中,运行结果 与使用