Three ways to implement multithreading in Java
In Java, there are three ways to implement multithreading. The first method: Inherit the thread class and override the run function. The second method: Implement the Runnable interface and rewrite the run function. The third method is to implement the callable interface and rewrite the call function. This article will explain how to implement multithreading in these three methods through an example. may refer to the need.
(1) Inherit the thread class and rewrite the run function.
Class XX extends thread{public
void Run () {
thread.sleep (1000) /thread sleeps 1000 milliseconds, sleep causes the thread to enter the block state, and frees the resource
}}
To open a thread:
Object. Start ()//boot thread, run function
(2) Implement Runnable interface, the code is as follows
Class Mythread implements Runnable {
private String name;
Public Mythread (String name) {
super ();
this.name = name;
@Override public
Void Run () {for
(int i = 0; i < i++) {
System.out.println ("Thread" +name+ "--->" +i) ;
}
}
}
public class Threaddemo {public
static void Main (string[] args) {
mythread a = new Mythread ("a");
Mythread B = new Mythread ("B");
Mythread C = new Mythread ("C");
New Thread (a). Start ();
New Thread (b). Start ();
New Thread (c). Start ();
}
}
(3) Implement callable interface, rewrite call function
Callable are interfaces similar to runnable, classes that implement callable interfaces, and classes that implement runnable are tasks that can be performed by other threads.
There are several differences between callable and runnable:
- Callable the method specified is call (), and runnable the method is run ().
- A callable task can return a value after execution, whereas a runnable task cannot return a value
- The call () method throws an exception, and the run () method cannot throw an exception.
- Run the callable task to get a future object, future represents the result of an asynchronous computation. It provides a way to check whether the calculation is complete, to wait for the calculation to complete, and to retrieve the results of the calculation. Through the future object, you can understand the execution of the task, cancel the execution of the task, and get the results of the task execution.
Java Callable code Example:
Class Taskwithresult implements callable<string> {
private int id;
public taskwithresult (int id) {
this.id = ID;
}
@Override public
String called () throws Exception {return ' result of
Taskwithresult ' + id;
}
}
public class Callabletest {public
static void Main (string[] args) throws Interruptedexception,
executionexception {
Executorservice exec = Executors.newcachedthreadpool ();
arraylist<future<string>> results = new arraylist<future<string>> (); Future is equivalent to a container for executor execution results
(int i = 0; i < i++) {
results.add exec.submit (New Taskwithresul T (i)));
for (future<string> fs:results) {
if (Fs.isdone ()) {
System.out.println (Fs.get ());
} else {
System.out.println ("Future result are not yet complete");
}
Exec.shutdown ();
}
Thank you for reading, I hope to help you, thank you for your support for this site!