In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about the ways to achieve threads in Java, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
The way to implement Thread in Java
At the core of the way multithreading is implemented in Java is the run () method, which ends up running through run () either way.
When Java was first released, version 1.0 of JDK provided two implementations, one to inherit the Thread class, and the other to implement the Runnable interface. Both ways are to rewrite the run () method and implement the specific business code in the run () method.
However, a common drawback of these two methods is that because the run () method does not return a value, the multithreaded reads implemented by these two methods cannot get the result of execution.
In order to solve this problem, a Callable interface is introduced in JDK 1.5.According to the type of return value set by generic V, its call () method is implemented, and the return result of thread execution can be obtained.
Although the call () method can get the return value, it needs to work with a Future to get the return result, which in turn is an interface that inherits Runnable. By referring to the source code, you can find that FutureTask, the implementation of Future, is still implemented in run () when doing specific business code execution.
FutureTask source code snippet: public void run () {
If (state! = NEW | |
! UNSAFE.compareAndSwapObject (this, runnerOffset
Null, Thread.currentThread ())
Return
Try {
Callable c = callable
If (c! = null & & state = = NEW) {
V result
Boolean ran
Try {
Result = c.call ()
Ran = true
} catch (Throwable ex) {
Result = null
Ran = false
SetException (ex)
}
If (ran)
Set (result)
}
} finally {
/ / runner must be non-null until state is settled to
/ / prevent concurrent calls to run ()
Runner = null
/ / state must be re-read after nulling runner to prevent
/ / leaked interrupts
Int s = state
If (s > = INTERRUPTING)
HandlePossibleCancellationInterrupt (s)
}
} Code example of Java multithreaded implementation: implementing public class ThreadTest {by inheriting the Thread class
Public static void main (String [] args) throws Exception {
Thread myThread = new MyThread ()
MyThread.setName ("MyThread-entends-Thread-test")
MyThread.start ()
}
}
Class MyThread extends Thread {
@ Override
Public void run () {
System.out.println ("Thread Name:" + Thread.currentThread () .getName ())
}
} implement public class ThreadTest {by implementing the Runnable interface
Public static void main (String [] args) throws Exception {
MyRunnableThread myRunnable = new MyRunnableThread ()
Thread myRunnableThread = new Thread (myRunnable)
MyRunnableThread.setName ("MyThread-implements-Runnable-test")
MyRunnableThread.start ()
}
}
Class MyRunnableThread implements Runnable {
@ Override
Public void run () {
System.out.println ("Thread Name:" + Thread.currentThread () .getName ())
}
} implement public class ThreadTest {by implementing the Callable interface
Public static void main (String [] args) throws Exception {
Callable myCallable = new MyCallableThread ()
FutureTask futureTask = new FutureTask (myCallable)
Thread myCallableThread = new Thread (futureTask)
MyCallableThread.setName ("MyThread-implements-Callable-test")
MyCallableThread.start ()
System.out.println ("Run by Thread:" + futureTask.get ())
/ / execute through thread pool
ExecutorService executorService = Executors.newCachedThreadPool ()
ExecutorService.submit (futureTask)
ExecutorService.shutdown ()
System.out.println ("Run by ExecutorService:" + futureTask.get ())
}
}
Class MyCallableThread implements Callable {
@ Override
Public String call () throws Exception {
Return Thread.currentThread () .getName ()
}
} these are the ways to implement threads in Java. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.