Método 1: Herdado da classe Thread
1. Crie uma subclasse herdada da classe Thread
2. Substitua o método run da classe Thread e declare a operação realizada por este thread no método run ()
3. Crie uma subclasse do Thread class Object
4. Chame o método start () por meio deste objeto.
Por exemplo:
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());
}
}
}
}
Método 2:
criar threads criando subclasses anônimas
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());
}
}
}
}*
}
}
Métodos comumente usados em Thread
1.start (): iniciar o thread atual, chamar o método run () do thread atual
2.run (): geralmente precisa reescrever o método na classe Thread e declarar a operação realizada pelo thread criada neste método
3.curreentThread (): método estático que retorna a thread executando o código atual de
4.getName (): Obtém o nome da thread atual
5.setName (): Define o nome da thread atual
6.yield (): a versão atual da cpu do poder executivo,
por exemplo:
@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 (): chama o método de junção do thread b no thread a, neste momento o thread a entra no estado de bloqueio, e o estado de bloqueio não terminará até que o thread b seja completamente executado
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): Deixe o thread atual "dormir" pelo número especificado de milissegundos e ficar em um estado bloqueado dentro do número especificado de milissegundos
@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 se o encadeamento atual ainda está ativo