java如何调用多线程_如何在 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方法。

一个线程可以干什么?看看它的主要方法:

voidstart()

voidinterrupt()

voidrun()

voidstop()

voiddestroy()

它的start方法就是使线程进入执行状态,使用了start方法以后,JVM就会调用run方法。当使用了start以后,不能立刻再次start。

几个要注意的静态方法:

static Thread currentThread()

static void dumpStack()

static int enumerate(Thread[] tarray)

static MapgetAllStackTraces()

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

到这里已经知道如何使用多线程了,那么就可以开始学习多线程了(是的这个只是留个多线程的印象而已)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值