|
彻底明白Java的多线程及线程的同步过程 |
|
![]() |
|
|
|
一. 实现多线程
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页: 上一页 1 [2] [3] 下一页 |
上一篇:eclipse正则表达式在编码过程中的运用 下一篇:Java高级--Java线程运行栈信息的获取
![](/images/xg.gif) |
相关文档 |
|
![](/images/xg.gif) |
发表评论 |
|
|
|
|