Method 1: Inherit from Thread
/** * Create Thread from Thread class * 1. Create a subclass of Thread * 2. Overwrite Thread run() --> declare the operations performed by this Thread in run() * 3. 4. Call start() * * example: Iterate over all even numbers up to 100 * */
//1. Create a subclass derived from Thread
class MyThread extends Thread {
//2. Run ()
@Override
public void run(a) {
for (int i = 0; i < 100; i++) {
if(i % 2= =0){
System.out.println(Thread.currentThread().getName() + ":"+ i); }}}}public class ThreadTest {
public static void main(String[] args) {
//3. Create objects that subclass Thread
MyThread t1 = new MyThread();
Call start() from this object :① Start the current thread ② call the current thread run()
t1.start();
We cannot start a thread by calling run() directly.
// t1.run();
// Start another thread and iterate through even numbers within 100. Do not allow threads already started () to execute. Will quote IllegalThreadStateException
// t1.start();
// We need to recreate a thread object
MyThread t2 = new MyThread();
t2.start();
// The following operations are still performed in the main thread.
for (int i = 0; i < 100; i++) {
if(i % 2= =0){
System.out.println(Thread.currentThread().getName() + ":" + i + "***********main()************");
}
}
}
}
```
```Java
// Create an anonymous subclass of Thread
new Thread(){
@Override
public void run(a) {
for (int i = 0; i < 100; i++) {
if(i % 2= =0){
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
}.start();
Copy the code
* 1. Start (): start the current Thread; Call the current thread's run() * 2.run (): * 3. currentThread(): static method, GetName (): Gets the name of the current thread * 5.setName (): sets the name of the current thread * 6.yield (): releases the execution of the current CPU * 7. Join (): thread A calls join() of thread B, and thread A blocks. Thread A does not stop blocking until thread B completes. * 8.stop (): Obsolete. When this method is executed, the current thread is forced to terminate. * 9. Sleep (Long Millitime): Lets the current thread "sleep" the specified Millitime millisecond. The current * thread is blocked for the specified Millitime millisecond. * * * Priority of the thread: * 1. * MAX_PRIORITY: 10 * min_priority: 1 * NORM_PRIORITY: 5 --> Default priority * 2. How to get and set the priority of the current thread: * getPriority(): get the priority of the thread * setPriority(int P): set the priority of the thread * * But only probabilistically, high-priority threads have a high probability of execution *. It does not mean that the low-priority thread executes only when the high-priority thread finishes. * * * /
class HelloThread extends Thread{
@Override
public void run(a) {
for (int i = 0; i < 100; i++) {
if(i % 2= =0) {// try {
// sleep(10);
// } catch (InterruptedException e) {
// e.printStackTrace();
/ /}
System.out.println(Thread.currentThread().getName() + ":" + Thread.currentThread().getPriority() + ":" + i);
}
// if(i % 20 == 0){
// yield();
/ /}}}public HelloThread(String name){
super(name); }}public class ThreadMethodTest {
public static void main(String[] args) {
HelloThread h1 = new HelloThread(Thread: "1");
// h1.setName("线程一");
// Set the threading priority
h1.setPriority(Thread.MAX_PRIORITY);
h1.start();
// Name the main thread
Thread.currentThread().setName("Main thread");
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
for (int i = 0; i < 100; i++) {
if(i % 2= =0){
System.out.println(Thread.currentThread().getName() + ":" + Thread.currentThread().getPriority() + ":" + i);
}
// if(i == 20){
// try {
// h1.join();
// } catch (InterruptedException e) {
// e.printStackTrace();
/ /}
/ /}
}
// System.out.println(h1.isAlive());}}Copy the code
Method 2: Implement the Runnable interface
/** * Create thread thread 2: implement Runnable * 1. Create a class that implements the Runnable interface. 2. Implement classes to implement abstract methods in Runnable: run() * 3. Create an object of the implementation class * 4. Pass this object as a parameter to the constructor of Thread, and create an object of Thread * 5. Compare the two ways to create a Thread by calling start() * * * on an object of the Thread class. * Under development: Preferred: implementation of Runnable interface * Reason: 1. The implementation approach does not have the limitations of the single inheritance of the class * 2. The implementation approach is better suited to handling situations where multiple threads have shared data. Thread implements Runnable (); Thread implements Runnable (); * * /
Create a class that implements the Runnable interface
class MThread implements Runnable{
//2. Implement the abstract method in Runnable: run()
@Override
public void run(a) {
for (int i = 0; i < 100; i++) {
if(i % 2= =0){
System.out.println(Thread.currentThread().getName() + ":"+ i); }}}}public class ThreadTest1 {
public static void main(String[] args) {
//3. Create an object for the implementation class
MThread mThread = new MThread();
//4. Pass this object as an argument to the constructor of Thread to create an object of Thread
Thread t1 = new Thread(mThread);
t1.setName(Thread 1 "");
Start ()--> call Runnable target run();
t1.start();
// Start another thread to traverse even numbers up to 100
Thread t2 = new Thread(mThread);
t2.setName(Thread 2 ""); t2.start(); }}Copy the code