Method 1: Inherited from the Thread class
1. Create a subclass inherited from the Thread class
2. Override the run method of the Thread class, and declare the operation performed by this thread in the run() method
3. Create a subclass of the Thread class Object
4. Call the start() method through this object
For example:
package com.itweiting.exer;
/**
* @Description 多线程的创建练习
* @User Administrator
* @Time 2021/3/29__9:32
* @
*/
class MyThread extends Thread{
@Override
public void run() {
for (int i = 0; i <100 ; i++) {
if (i%2==0){
System.out.println(i+Thread.currentThread().getName());
}
}
}
}
class Mythreadtwo extends Thread{
@Override
public void run() {
for (int i = 0; i <100 ; i++) {
if (i%2!=0){
System.out.println(i+Thread.currentThread().getName());
}
}
}
}
public class ThreadDemo {
public static void main(String[] args) {
//
MyThread t1=new MyThread();
t1.start();
Mythreadtwo t2=new Mythreadtwo();
t2.start();
for (int i = 0; i <100 ; i++) {
if (i%2==0){
System.out.println(i+Thread.currentThread().getName());
}
}
}
}
Method 2:
Create threads by creating anonymous subclasses
package com.itweiting.exer;
/**
* @Description 多线程的创建练习
* @User Administrator
* @Time 2021/3/29__9:32
* @
*/
class MyThread extends Thread{
@Override
public void run() {
for (int i = 0; i <100 ; i++) {
if (i%2==0){
System.out.println(i+Thread.currentThread().getName());
}
}
}
}
class Mythreadtwo extends Thread{
@Override
public void run() {
for (int i = 0; i <100 ; i++) {
if (i%2!=0){
System.out.println(i+Thread.currentThread().getName());
}
}
}
}
public class ThreadDemo {
public static void main(String[] args) {
//
MyThread t1=new MyThread();
t1.start();
Mythreadtwo t2=new Mythreadtwo();
t2.start();
*//创建匿名子类的方式创建一个线程
new Thread(){
@override
punlic void run(){
for (int i = 0; i <100 ; i++) {
if (i%2!=0){
System.out.println(i+Thread.currentThread().getName());
}
}
}
}*
}
}
Commonly used methods in Thread
1.start(): start the current thread, call the run() method of the current thread
2.run(): usually need to rewrite the method in the Thread class, and declare the operation performed by the created thread in this method
3.curreentThread (): static method that returns the thread executing the current code of
4.getName (): Gets the current thread's name
5.setName (): Sets the current thread's name
6.yield (): the current release of executive power cpu
for example, :
@Override
public void run() {
for (int i = 0; i <100 ; i++) {
if (i%2!=0){
System.out.println(i+Thread.currentThread().getName());
}
if (i%20==0){
yield();
}
}
}
7.join(): call the join method of thread b in thread a, at this time thread a enters the blocking state, and the blocking state will not end until thread b is completely executed
for (int i = 0; i <100 ; i++) {
if (i%2==0){
System.out.println(i+Thread.currentThread().getName());
}
if (i==10){
//当主线程执行到10后,开始执行分线程,当分线程执行完之后再执行主线程
try{
t2.join();//会报异常
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
8.sleep(long millitime): Let the current thread "sleep" for the specified number of milliseconds, and be in a blocked state within the specified number of milliseconds
@Override
public void run() {
for (int i = 0; i <100 ; i++) {
if (i%2==0){
try {
sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(i+Thread.currentThread().getName());
}
}
}
}.start();
9.isAlive(): Determine whether the current thread is still alive