Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Several Writing methods of Java Multithreading

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Java multithreading is often used in development. This paper briefly summarizes several writing methods, which are inheriting Thread method, implementing Runnable interface and implementing Callable interface.

1. Inherit the Thread method

Class TestThread extends Thread {String name; public TestThread (String name) {this.name=name;} @ Override public void run () {for (int I = 0; I)

< 6; i++) { System.out.println(this.name+":"+i); } }} main方法调用: Thread启动有两个方法,一个是start()方法,一个是run()方法,但是直接调用run方法时线程不会交替运行,而是顺序执行,只有用start方法时才会交替执行 TestThread tt1 = new TestThread("A"); TestThread tt2 = new TestThread("B"); tt1.start(); tt2.start(); 运行结果:

two。 To implement the Runnable interface, there are many ways to write it

2.1 external classes

Class TestRunnable implements Runnable {String name; public TestRunnable (String name) {this.name=name;} @ Override public void run () {for (int I = 0; I)

< 6; i++) { System.out.println(this.name+":"+i); } }} 调用: TestRunnable tr1 = new TestRunnable("C"); TestRunnable tr2 = new TestRunnable("D"); new Thread(tr1).start(); new Thread(tr2).start(); 2.2匿名内部类方式 new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub } }).start(); 2.3 Lamda表达式,jdk1.8,只要是函数式接口,都可以使用Lamda表达式或者方法引用 new Thread(()->

{for (int I = 0; I)

< 6; i++) { System.out.println(i); } }).start(); 2.4ExecutorService创建线程池的方式 class TestExecutorService implements Runnable{String name;public TestExecutorService(String name){ this.name=name;} @Override public void run() { for (int i = 0; i < 6; i++) { System.out.println(this.name+":"+i); } }} 调用:可以创建固定个数的线程池 ExecutorService pool = Executors.newFixedThreadPool(2); TestExecutorService tes1 = new TestExecutorService("E"); TestExecutorService tes2 = new TestExecutorService("F"); pool.execute(tes1); pool.execute(tes2); pool.shutdown(); 运行结果跟2.1差不多

3. Implement the Callable interface and return the result

/ / Callable provides returned data and returns different types of class TestCallable implements Callable {private int ticket = 5; @ Override public String call () throws Exception {for (int I = 0; I) as needed

< 5; i++) { if(this.ticket>

0) System.out.println ("Buy tickets, ticket=" + this.ticket--);} return "tickets are sold out";}}

Call:

Callable tc = new TestCallable (); FutureTask task = new FutureTask (tc); new Thread (task). Start (); try {System.out.println (task.get ()); / / get the return value} catch (InterruptedException | ExecutionException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} run result:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report