如何在 JAVA 中使用多线程
public class Thread
extends Object
implements Runnable
JAVA中多线程的实现依赖于Thread类。Thread就是一个线程,但是创建了线程并不代表就开始了一个线程(联系操作系统中提到的线程的状态,创建、运行、阻塞等)。
看Thread类之前要先了解一下Runnable接口。
Runnable接口只有一个方法:void run()。也就是说,实现该接口只需要写一个run方法。当你开始执行一个线程的时候,实际上就是在执行线程的run方法。
Thread类的构造方法如下:
Constructor Description
Thread() Allocates a new Thread object.
Thread(Runnable target) Allocates a new Thread object.
Thread(Runnable target, String name) Allocates a new Thread object.
Thread(String name) Allocates a new Thread object.
Thread(ThreadGroup group, Runnable target) Allocates a new Thread object.
Thread(ThreadGroup group, Runnable target, String name) Allocates a new Thread object so that it has target as its run object, has the specified name as its name, and belongs to the thread group referred to by group.
Thread(ThreadGroup group, Runnable target, String name, long stackSize) Allocates a new Thread object so that it has target as its run object, has the specified name as its name, and belongs to the thread group referred to by group, and has the specified stack size.
Thread(ThreadGroup group, Runnable target, String name, long stackSize, boolean inheritThreadLocals) Allocates a new Thread object so that it has target as its run object, has the specified name as its name, belongs to the thread group referred to by group, has the specified stackSize, and inherits initial values for inheritable thread-local variables if inheritThreadLocals is true.
Thread(ThreadGroup group, String name) Allocates a new Thread object.
public Thread(ThreadGroup group, Runnable target, String name),三个参数分别是线程组,实现了Runnable的类和线程名。Thread()就相当于三个参数都是null,后几个也类似。
另外还有一些get和set方法。
一个线程可以干什么?看看它的主要方法:
void start()
void interrupt()
void run()
void stop()
void destroy()
它的start方法就是使线程进入执行状态,使用了start方法以后,JVM就会调用run方法。当使用了start以后,不能立刻再次start。
几个要注意的静态方法:
static Thread currentThread()
static void dumpStack()
static int enumerate(Thread[] tarray)
static Map<Thread,StackTraceElement[]> getAllStackTraces()
static boolean holdsLock(Object obj)
static boolean interrupted()
static void sleep(long millis)
如果希望创建一个有意义的可执行的线程,那就需要一个有意义的run方法。
一:创建一个Thread类的子类,覆写Thread的run方法。
二:创建Thread时传递一个实现了runnable的类(也可利用匿名类)。
线程是并行执行而非顺序的,所以要注意线程的执行顺序和start的顺序是没有关联的。
class RedThread implements Runnable{
int count = 0;
public void run(){
for(;count < 10; count++)
System.out.println(Thread.currentThread().getName() + ": "+ count);;
}
}
class YellowThread extends Thread{
int count = 0;
public void run(){
for(;count < 10; count++)
System.out.println(this.getName() + ": " + count);
}
}
public class Test {
public static void main(String[] args) {
Thread blueThread= new Thread("blueThread") {
int count = 0;
public void run() {
for(;count < 10; count++)
System.out.println(this.getName() + ": " + count);
}
};
Thread redThread = new Thread(new RedThread(),"redThread");
YellowThread yellowThread = new YellowThread();
yellowThread.setName("yellowThread");
yellowThread.start();
redThread.start();
blueThread.start();
}
}
输出结果:
yellowThread: 0
redThread: 0
yellowThread: 1
redThread: 1
yellowThread: 2
redThread: 2
yellowThread: 3
redThread: 3
yellowThread: 4
redThread: 4
yellowThread: 5
redThread: 5
yellowThread: 6
redThread: 6
yellowThread: 7
redThread: 7
yellowThread: 8
redThread: 8
yellowThread: 9
blueThread: 0
blueThread: 1
redThread: 9
blueThread: 2
blueThread: 3
blueThread: 4
blueThread: 5
blueThread: 6
blueThread: 7
blueThread: 8
blueThread: 9
到这里已经知道如何使用多线程了,那么就可以开始学习多线程了(是的这个只是留个多线程的印象而已)