欢迎光临
我们一直在努力

Java入门笔记6_线程-JSP教程,Java基础

建站超值云服务器,限时71元/月

1. 多线程

1.1 创建线程类

在java中可以简单的从thread类中继承创建自己的线程类:

public class myfirstthread extends thread {

public void run() { . . .}

}

说明:

(1) thread类位是java.lang包中,所以可以不用显示import;

(2) 从thread类中继承下来的类最好重载run()方法,以运行需要的代码;

可以按以下方法实例化并运行线程:

myfirstthread amft = new myfirstthread();

amft.start();

说明:

(3) 实例化线程类后,系统会初始化一些参数,主要是为线程创建名称,把新的线程加入指定的线程组,初始化线程运行需要的内存空间,指定新线程的优先级别,指定它的守候线程;

(4) start方法是thread类中的方法,它会调用run方法,在新的线程中运行指定的代码;

(5) 除了start方法外,从thread继承下来的类还具有其它一些主要的方法:stop,suspend,resume等;

以下是一个完整的thread派生类:

1: public class complexthread extends thread {

2: private int delay;

3:

4: complexthread(string name, float seconds) {

5: super(name);

6: delay = (int) seconds * 1000; // delays are in milliseconds

7: start(); // start up ourself!

8: }

9:

10: public void run() {

11: while (true) {

12: system.out.println(thread.currentthread().getname());

13: try {

14: thread.sleep(delay);

15: } catch (interruptedexception e) {

16: return;

17: }

18: }

19: }

20:

21: public static void main(string argv[]) {

22: new complexthread("one potato", 1.1f);

23: new complexthread("two potato", 1.3f);

24: new complexthread("three potato", 0.5f);

25: new complexthread("four", 0.7f);

26: }

27: }

1.2 runable接口

创建多线程运行指定代码的另一种方法是,在创建类时implement runable这个接口:

public class mysecondthread extends importantclass implements runnable {

public void run() {. . .}

}

说明:

(1) 该类implement runable接口,就表明有意图运行在单独的线程中,thread也是implement runable接口的;

(2) implement runalbe接口至少需要实现run方法;

以下是创建新线程运行该类的实例:

mysecondthread amst = new mysecondthread();

thread athread = new thread(amst);

athread.start();

说明:

(3) thread类有多个构造函数thread()、thread(runable target)等,本例中用的就是第二个构造函数,它有一个runable类型的函数,所以要在多线程中运行的实例的类必须是implement runable的;

(4) athead.start()方法调用thread实例的中的target.run方法,本例中就是mysecondthread实例中的run方法;

(5) thread构造函数还可以指定线程名,运行所需的stack,线程所属的组等;

为了防止线程非正常结束,需要将start方法置入try…catch中,如:

try{

mythread.start();

}catch(threaddeath atd){

system.out.println("end thread");

throw atd;

}

在这个例子中将捕获threaddeath异常,处理后重新抛出该异常,以便java执行stop方法,进行资源等清理工作。

1.3 线程的优先级

多个线程的执行是有一定的优先级别的,对于下面这个例子:

public class runnablepotato implements runnable {

public void run() {

while (true)

system.out.println(thread.currentthread().getname());

}}

public class potatothreadtester {

public static void main(string argv[]) {

runnablepotato arp = new runnablepotato();

thread t1 = new thread(arp, "one potato");

thread t2 = new thread(arp, "two potato");

t1.start();

t2.start();

}}

对于非抢占式的系统,上例中的第一个线程会一直运行,第二个线程没有机会运行;对于抢占式的系统,这二人线程会交替运行。

为了让多线程在非抢占式中运行,最好在run方法中加入以下语句:

thread.yield()

public void run() {

while (true)

system.out.println(thread.currentthread().getname());

thread.yield()

}

thread.yield会将当前线程暂时让位一小段时间,让其它的线程有机会运行,过了这段时间后,该线程继承运行。上述功能也可以用thread.sleep()方法实现。

在java中有优先级别可以从1到10,其中1可以用thread.min_priority表示,5可以用thread.norm_priority,10可以用thread.max_priority表示,新建一个线程默认的级别是thread.norm_priority。也可以使用setpriority方法改变线程的优先级别,如t1.setpriority(t2.getpriority + 1)。

与yield方法相反的是join方法,它表示一直要等到指定的线程运行完毕,如:

try{ t.join();} catch (interruptedexception ignored) { }

表示要等到线程t运行完毕后,再执行下一步操作。这种情况比较少见。

1.4 synchronized

为了保证某个方法或者对象某个时刻只能被一个方法访问,那就需要使用synchronized关键字。

如:

public synchronized void countme() {

crucialvalue += 1;

}

就表示countme这个方法中的操作是一个原子操作,+= 要执行三个步骤,使用synchronized后,这三个步骤是具有原子性,即在三个步骤完成前,其它对于crucialvalue的访问都将被拒绝,即可保证countme的线程安全。

另一个例子:

synchronized(p) {

safex = p.x();

safey = p.y();

}

表示在block范围内锁定p对象,不许其它程序修改p对象中的值。

以上代码的作用都是保护某个对象内的变量不能同时被多个线程访问,下面介绍如何保护class variable的线程安全:

public class staticcounter {

private static int crucialvalue;

public void countme() {

synchronized(getclass()) {

crucialvalue += 1;

} } }

说明:

(1) 在这个例子中,crucialvalue是private并且static,这表示它可以被该类的所有实例访问;

(2) synchronized使用getclass方法获取类名,而不能直接使用staticcounter

(3) 如果crucialvalue是public的,那么修改代码成:

synchronized(class.forname("staticcounter")) {

staticcounter.crucialvalue += 1;

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » Java入门笔记6_线程-JSP教程,Java基础
分享到: 更多 (0)

相关推荐

  • 暂无文章