多线程知识

来源:互联网 发布:jade软件安装 编辑:程序博客网 时间:2024/05/27 16:40

一:单线程示例

package Thread;import java.io.PrintWriter;public class WithoutThread{static PrintWriter out = new PrintWriter(System.out,true);public static void main(String[] args) {NoThreadPseudoIO pseudo = new NoThreadPseudoIO();pseudo.run();showElapsedTime("Anthor task starts");}static long baseTime =System.currentTimeMillis();static void showElapsedTime(String message){long elapsedTime = System.currentTimeMillis()-baseTime;out.println(message + " at " +(elapsedTime/1000.0)+" seconds");}}class NoThreadPseudoIO{int data=-1;NoThreadPseudoIO(){WithoutThread.showElapsedTime("NoThreadPseudoIO created");}public void run(){WithoutThread.showElapsedTime("NoThreadPseudoIO starts");try{Thread.sleep(10000);data=99;WithoutThread.showElapsedTime("NoThreadPseudoIO finishes");}catch(InterruptedException e){}}}

结果:

NoThreadPseudoIO created at 0.0 seconds
NoThreadPseudoIO starts at 0.0 seconds
NoThreadPseudoIO finishes at 10.0 seconds
Anthor task starts at 10.0 seconds


二:多线程的生成

    1:作为Thread子类

          这个子类定义自己的run()方法来覆盖Thread类的run()方法,这个run()方法局势任务运行的地方

          run()方法是java运行时为了启动线程而调用的第一个用户定义方法,然后new词句生成这个子类的实例,并调用线程的start方法执行run()方法。

代码:

package Thread;import java.io.PrintWriter;public class WithThread {static PrintWriter out = new PrintWriter(System.out,true);public static void main(String[] args) {ThreadPseudoIO pseudo = new ThreadPseudoIO();pseudo.start();showElapsedTime("Anthor task starts");}static long baseTime =System.currentTimeMillis();static void showElapsedTime(String message){long elapsedTime = System.currentTimeMillis()-baseTime;out.println(message + " at " +(elapsedTime/1000.0)+" seconds");}}class ThreadPseudoIO extends Thread{int data=-1;ThreadPseudoIO(){WithoutThread.showElapsedTime("ThreadPseudoIO created");}public void run(){WithoutThread.showElapsedTime("ThreadPseudoIO starts");try{Thread.sleep(10000);data=999;WithoutThread.showElapsedTime("ThreadPseudoIO finishes");}catch(InterruptedException e){}}}

结果:

ThreadPseudoIO created at 0.0 seconds
ThreadPseudoIO starts at 0.0 seconds
Anthor task starts at 0.0 seconds
ThreadPseudoIO finishes at 10.0 seconds



    2:声明一个实现Runnable接口的类

         Runable接口需要一个方法来实现,即run()方法

         首先用new语句生成这个了类的实例

         然后用另一个new语句生成Thread实例

          最后调用这个线程实例的start()方法 ,于是开始运行run()方法中定义的任务。

代码:

import java.io.PrintWriter;public class RunnableThread {static PrintWriter out = new PrintWriter(System.out,true);public static void main(String[] args) {RunnablePseudoIO pseudo = new RunnablePseudoIO();Thread thread = new Thread(pseudo);thread.start();showElapsedTime("Anthor task starts");}static long baseTime =System.currentTimeMillis();static void showElapsedTime(String message){long elapsedTime = System.currentTimeMillis()-baseTime;out.println(message + " at " +(elapsedTime/1000.0)+" seconds");}}class RunnablePseudoIO implements Runnable{int data= -1;RunnablePseudoIO(){RunnableThread.showElapsedTime("RunnablePseudoIO creates");}public void run() {RunnableThread.showElapsedTime("RunnablePseudoIO starts");try {Thread.sleep(10000);} catch (InterruptedException e) {e.printStackTrace();}data=999;RunnableThread.showElapsedTime("RunnablePseudoIO finishes");}}


结果:

RunnablePseudoIO creates at 0.0 seconds
RunnablePseudoIO starts at 0.016 seconds
Anthor task starts at 0.016 seconds
RunnablePseudoIO finishes at 10.016 seconds

原创粉丝点击