Varias formas de crear múltiples subprocesos en java

rápido

Cabe señalar que, independientemente del método que se utilice para crear un hilo de inicio, se le debe dar un nombre, que es útil para la resolución de problemas y el monitoreo del sistema de diagnóstico; de lo contrario, es imposible conocer intuitivamente el propósito de un hilo al diagnosticar problemas.

Heredar la clase Thread

Clase MyThread


public class MyThread extends Thread {
    
    
	public MyThread() {
    
    
	}

	//private String name;
	MyThread(String name) {
    
    
		//this.name = name;
		super(name);
	}

	@Override
	public void run() {
    
    
		for (int i = 0; i < 20; i++) {
    
    
			/**
			 * Thread.currentThread() 返回当前线程的引用
			 * this.getName()  返回当前线程的名字 外部方法怎么setName设置线程名字,内部就可以获取线程名字
			 */
			System.out.println(this.getName() + " : " + i);

		}
	}
}
public class Main {
    
    
	/**
	 * 继承方式实现线程
	 *
	 * @param args
	 */
	public static void main(String[] args) {
    
    
		MyThread myThread = new MyThread();
		myThread.setName("我是main线程");
		myThread.run();
	}
}

Implementar la interfaz Runnable

Mi tarea

public class MyTask implements Runnable {
    
    

	@Override
	public void run() {
    
    
		for (int i = 0; i < 20; i++) {
    
    
			//获取线程名字
			System.out.println(Thread.currentThread().getName()+i);
		}
	}
}

Método principal



public class Main {
    
    
	/**
	 * 实现方式创建线程
	 */
	public static void main(String[] args) {
    
    
		MyTask myTask = new MyTask();
		Thread thread = new Thread(myTask);
		thread.setName("Runnable");// 设置名字
		String name = thread.getName(); //获取名字
		boolean interrupted = thread.isInterrupted();//如果这个线程被中断就返回true
//		thread.checkAccess();
		ClassLoader contextClassLoader = thread.getContextClassLoader();//返回此线程的上下文类加载器
		long id = thread.getId();//返回此线程的标识符
		int priority = thread.getPriority();//返回线程的优先级

		Thread.State state = thread.getState(); //返回线程的状态 , 具体的点State枚举看源码
		ThreadGroup threadGroup = thread.getThreadGroup();//返回线程组

		boolean alive = thread.isAlive(); //测试线程是否活动
		boolean daemon = thread.isDaemon(); //测试线程是否是守护线程

		thread.start();

	}
}



Creación de clases internas anónimas


/**
 * 匿名内部类方式创建
 */
public class Main {
    
    

	public static void main(String[] args) {
    
    

		//1.第一种方式 继承方式
		new Thread() {
    
    
			@Override
			public void run() {
    
    
				//任务代码
				for (int i = 0; i < 20; i++) {
    
    
					System.out.println(Thread.currentThread().getName() + ":" + i);
				}
			}
		}.start();

//2.第二种方式 实现方法
		new Thread(new Runnable() {
    
    
			@Override
			public void run() {
    
    
				for (int i = 0; i < 20; i++) {
    
    
					System.out.println(Thread.currentThread().getName() + ":" + i);
				}
			}
		}).start();

		//主线程代码
		for (int i = 0; i < 20; i++) {
    
    
			System.out.println(Thread.currentThread().getName() + ":" + i);
		}
	}
}



Supongo que te gusta

Origin blog.csdn.net/qq_41489540/article/details/109109609
Recomendado
Clasificación