一. 实现多线程
1. 虚假的多线程
例1:
public class testthread { int i=0, j=0; public void go(int flag){ while(true){ try{ thread.sleep(100); } catch(interruptedexception e){ system.out.println("interrupted"); } if(flag==0) i++; system.out.println("i=" + i); } else{ j++; system.out.println("j=" + j); } } } public static void main(string[] args){ new testthread().go(0); new testthread().go(1); }}
上面程序的运行结果为:
i=1
i=2
i=3
。。。
结果将一直打印出i的值。我们的意图是当在while循环中调用sleep()时,另一个线程就将起动,打印出j的值,但结果却并不是这样。关于sleep()为什么不会出现我们预想的结果,在下面将讲到。
2. 实现多线程
通过继承class thread或实现runnable接口,我们可以实现多线程
2.1 通过继承class thread实现多线程
class thread中有两个最重要的函数run()和start()。
1) run()函数必须进行覆写,把要在多个线程中并行处理的代码放到这个函数中。
2) 虽然run()函数实现了多个线程的并行处理,但我们不能直接调用run()函数,而是通过调用start()函数来调用run()函数。在调用start()的时候,start()函数会首先进行与多线程相关的初始化(这也是为什么不能直接调用run()函数的原因),然后再调用run()函数。
例2:
public class testthread extends thread{ private static int threadcount = 0; private int threadnum = ++threadcount; private int i = 5; public void run(){ while(true){ try{ thread.sleep(100); } catch(interruptedexception e){ system.out.println("interrupted"); } system.out.println("thread " + threadnum + " = " + i); if(--i==0) return; } } public static void main(string[] args){ for(int i=0; i<5; i++) new testthread().start(); }}
运行结果为:
thread 1 = 5
thread 2 = 5
thread 3 = 5
thread 4 = 5
thread 5 = 5
thread 1 = 4
thread 2 = 4
thread 3 = 4
thread 4 = 4
thread 1 = 3
thread 2 = 3
thread 5 = 4
thread 3 = 3
thread 4 = 3
thread 1 = 2
thread 2 = 2
thread 5 = 3
thread 3 = 2
thread 4 = 2
thread 1 = 1
thread 2 = 1
thread 5 = 2
thread 3 = 1
thread 4 = 1
thread 5 = 1
从结果可见,例2能实现多线程的并行处理。
**:在上面的例子中,我们只用new产生thread对象,并没有用reference来记录所产生的thread对象。根据垃圾回收机制,当一个对象没有被reference引用时,它将被回收。但是垃圾回收机制对thread对象“不成立”。因为每一个thread都会进行注册动作,所以即使我们在产生thread对象时没有指定一个reference指向这个对象,实际上也会在某个地方有个指向该对象的reference,所以垃圾回收器无法回收它们。
3) 通过thread的子类产生的线程对象是不同对象的线程
class testsynchronized extends thread{ public testsynchronized(string name){ super(name); } public synchronized static void prt(){ for(int i=10; i<20; i++){ system.out.println(thread.currentthread().getname() + " : " + i); try{ thread.sleep(100); } catch(interruptedexception e){ system.out.println("interrupted"); } } } public synchronized void run(){ for(int i=0; i<3; i++){ system.out.println(thread.currentthread().getname() + " : " + i); try{ thread.sleep(100); } catch(interruptedexception e){ system.out.println("interrupted"); } } }}public class testthread{ public static void main(string[] args){ testsynchronized t1 = new testsynchronized("t1"); testsynchronized t2 = new testsynchronized("t2"); t1.start(); t1.start(); //(1) //t2.start(); (2) }}
运行结果为:
t1 : 0
t1 : 1
t1 : 2
t1 : 0
t1 : 1
t1 : 2
由于是同一个对象启动的不同线程,所以run()函数实现了synchronized。如果去掉(2)的注释,把代码(1)注释掉,结果将变为:
t1 : 0
t2 : 0
t1 : 1
t2 : 1
t1 : 2
t2 : 2
由于t1和t2是两个对象,所以它们所启动的线程可同时访问run()函数。
2.2 通过实现runnable接口实现多线程
如果有一个类,它已继承了某个类,又想实现多线程,那就可以通过实现runnable接口来实现。
1) runnable接口只有一个run()函数。
2) 把一个实现了runnable接口的对象作为参数产生一个thread对象,再调用thread对象的start()函数就可执行并行操作。如果在产生一个thread对象时以一个runnable接口的实现类的对象作为参数,那么在调用start()函数时,start()会调用runnable接口的实现类中的run()函数。
例3.1:
public class testthread implements runnable{ private static int threadcount = 0; private int threadnum = ++threadcount; private int i = 5; public void run(){ while(true){ try{ thread.sleep(100); } catch(interruptedexception e){ system.out.println("interrupted"); } system.out.println("thread " + threadnum + " = " + i); if(--i==0) return; } } public static void main(string[] args){ for(int i=0; i<5; i++) new thread(new testthread()).start(); //(1) }}
运行结果为:
thread 1 = 5
thread 2 = 5
thread 3 = 5
thread 4 = 5
thread 5 = 5
thread 1 = 4
thread 2 = 4
thread 3 = 4
thread 4 = 4
thread 4 = 3
thread 5 = 4
thread 1 = 3
thread 2 = 3
thread 3 = 3
thread 4 = 2
thread 5 = 3
thread 1 = 2
thread 2 = 2
thread 3 = 2
thread 4 = 1
thread 5 = 2
thread 1 = 1
thread 2 = 1
thread 3 = 1
thread 5 = 1
例3是对例2的修改,它通过实现runnable接口来实现并行处理。代码(1)处可见,要调用testthread中的并行操作部分,要把一个testthread对象作为参数来产生thread对象,再调用thread对象的start()函数。
3) 同一个实现了runnable接口的对象作为参数产生的所有thread对象是同一对象下的线程。
例3.2:
package mypackage1;public class testthread implements runnable{ public synchronized void run(){ for(int i=0; i<5; i++){ system.out.println(thread.currentthread().getname() + " : " + i); try{ thread.sleep(100); } catch(interruptedexception e){ system.out.println("interrupted"); } } } public static void main(string[] args){ testthread testthread = new testthread(); for(int i=0; i<5; i++) //new thread(testthread, "t" + i).start(); (1) new thread(new testthread(), "t" + i).start(); (2) }}
运行结果为:
t0 : 0
t1 : 0
t2 : 0
t3 : 0
t4 : 0
t0 : 1
t1 : 1
t2 : 1
t3 : 1
t4 : 1
t0 : 2
t1 : 2
t2 : 2
t3 : 2
t4 : 2
t0 : 3
t1 : 3
t2 : 3
t3 : 3
t4 : 3
t0 : 4
t1 : 4
t2 : 4
t3 : 4
t4 : 4
由于代码(2)每次都是用一个新的testthread对象来产生thread对象的,所以产生出来的thread对象是不同对象的线程,所以所有thread对象都可同时访问run()函数。如果注释掉代码(2),并去掉代码(1)的注释,结果为:
t0 : 0
t0 : 1
t0 : 2
t0 : 3
t0 : 4
t1 : 0
t1 : 1
t1 : 2
t1 : 3
t1 : 4
t2 : 0
t2 : 1
t2 : 2
t2 : 3
t2 : 4
t3 : 0
t3 : 1
t3 : 2
t3 : 3
t3 : 4
t4 : 0
t4 : 1
t4 : 2
t4 : 3
t4 : 4
由于代码(1)中每次都是用同一个testthread对象来产生thread对象的,所以产生出来的thread对象是同一个对象的线程,所以实现run()函数的同步。
二. 共享资源的同步
1. 同步的必要性
例4:
class seq{ private static int number = 0; private static seq seq = new seq(); private seq() {} public static seq getinstance(){ return seq; } public int get(){ number++; //(a) return number; //(b) }}public class testthread{ public static void main(string[] args){ seq.getinstance().get(); //(1) seq.getinstance().get(); //(2) }}
上面是一个取得序列号的单例模式的例子,但调用get()时,可能会产生两个相同的序列号:
当代码(1)和(2)都试图调用get()取得一个唯一的序列。当代码(1)执行完代码(a),正要执行代码(b)时,它被中断了并开始执行代码(2)。一旦当代码(2)执行完(a)而代码(1)还未执行代码(b),那么代码(1)和代码(2)就将得到相同的值。
2. 通过synchronized实现
闽公网安备 35060202000074号