If you have any comments, criticisms or praise: please send me a letter climber_2002@sina.com
Java threads are implemented by thread, generally we create threads to do a complex operation, and then in the main thread of the operation of the results of processing, but the thread of the run function does not return the value, then we calculate the results, how to notify the other threads, This article describes several ways to return information.
One. Creation of Java Threads
There are two ways to create a thread, one is to inherit the thread class, the other is to implement Runnable, and then pass it to a thread's constructor, as follows (suppose we want to compute 1 to 10000 in a thread):
1. Inherit thread:
public class AddThread extends Thread {
public void run() {
int result = 0;
for(int i = 1; i <= 10000; i++) {
result += i;
}
}
}
Run Addthread:
Addthread thread = new Addthread ();
Thread.Start ();
2. Implement Interface runnable:
public class Add implements Runnable {
public void run() {
int result = 0;
for(int i = 1; i <= 10000; i++) {
result += i;
}
}
}
Run this thread: thread thread = new Thread (new Add ());
Thread.Start ();