前言:
当前看官们对“多线程程序的实现方式”大致比较重视,朋友们都需要了解一些“多线程程序的实现方式”的相关内容。那么小编也在网摘上搜集了一些关于“多线程程序的实现方式””的相关内容,希望我们能喜欢,同学们快快来了解一下吧!多线程三种方式实现
一、继承Thread
a、直接使用run运行public class MyThread extends Thread{ private String name; public MyThread(String name){ this.name = name; } @Override public void run() { for (int i = 0; i < 3; i++) { System.out.println(this.name +"-->"+ i); } }}class ThreadDemo{ public static void main(String[] args) { MyThread threadA = new MyThread("线程A"); MyThread threadB = new MyThread("线程B"); MyThread threadC = new MyThread("线程C"); threadA.run(); threadB.run(); threadC.run(); }}结果线程A-->0线程A-->1线程B-->0线程B-->1线程C-->0线程C-->1分析:此时并未启动多线程,多线程启动的唯一方法是Thread中的start()方法b、使用start()方法class ThreadDemo{ public static void main(String[] args) { MyThread threadA = new MyThread("线程A"); MyThread threadB = new MyThread("线程B"); MyThread threadC = new MyThread("线程C"); threadA.start(); threadB.start(); threadC.start(); }}结果:线程A-->0线程C-->0线程A-->1线程C-->1线程B-->0线程B-->1注:每次运行结果不一样疑问:为什么多线程启动方法不是run()而是start(),查看源码
总结:start()中包含native start0()方法。在java系统中,有一套JNI(java native interface)技术,java系统可以方便调用操作系统的资源。所以,多线程启动,唯一方法是Thread.start()
二、实现Runnable
public class MyRunnableTest implements Runnable{ private String name; public MyRunnableTest(String name){ this.name = name; } @Override public void run() { for (int i = 0; i < 3; i++) { System.out.println(Thread.currentThread().getName()+"->"+this.name +"-->"+ i); } }}class MyRunnableDemo{ public static void main(String[] args) { MyRunnableTest myRunnableTestA = new MyRunnableTest("线程A"); MyRunnableTest myRunnableTestB = new MyRunnableTest("线程B"); MyRunnableTest myRunnableTestC = new MyRunnableTest("线程C"); new Thread(myRunnableTestA).start(); new Thread(myRunnableTestB).start(); new Thread(myRunnableTestC).start(); }}结果:Thread-0->线程A-->0Thread-0->线程A-->1Thread-1->线程B-->0Thread-1->线程B-->1Thread-2->线程C-->0Thread-2->线程C-->1注:每次运行结果不一样小结:上面提到过,多线程启动时Thread.start()方法。
三、实现Callable
public class CallableTest implements Callable<String> { @Override public String call() throws Exception { System.out.println("线程" + Thread.currentThread().getName()); return Thread.currentThread().getName() + "启动"; }}class CallableDemo { public static void main(String[] args) throws ExecutionException, InterruptedException { CallableTest callableTestA = new CallableTest(); CallableTest callableTestB = new CallableTest(); CallableTest callableTestC = new CallableTest(); FutureTask<String> futureTaskA = new FutureTask<String>(callableTestA); FutureTask<String> futureTaskB = new FutureTask<String>(callableTestB); FutureTask<String> futureTaskC = new FutureTask<String>(callableTestC); new Thread(futureTaskA).start(); new Thread(futureTaskB).start(); new Thread(futureTaskC).start(); futureTaskA.get(); futureTaskB.get(); futureTaskC.get(); System.out.println("A线程返回结果" + futureTaskA.get()); System.out.println("B线程返回结果" + futureTaskB.get()); System.out.println("C线程返回结果" + futureTaskC.get()); }}结果:线程Thread-0线程Thread-2线程Thread-1A线程返回结果Thread-0启动B线程返回结果Thread-1启动C线程返回结果Thread-2启动
版权声明:
本站文章均来自互联网搜集,如有侵犯您的权益,请联系我们删除,谢谢。
标签: #多线程程序的实现方式