Java基础 --- 创建线程 Thread Creation

如何创建线程

使用Thread类

  • 创建一个类继承Thread类
  • 重写该接口的run()方法,该run()方法的方法体是该线程的线程执行体
package SimpleThread;

public class SimpleThread extends Thread {
    
    
	static int count = 0;
	//重写run方法, 具体业务逻辑的实现
	@Override
	public void run() {
    
    
		while(true) {
    
    
			System.out.println("Hello: " + count);
			try {
    
    
				Thread.sleep(1000);
				count++;
			} catch (InterruptedException e) {
    
    
				e.printStackTrace();
			}
		}
	}
	
	
	public static void main(String[] args) {
    
    
		//创建Cat对象,可以当做线程使用
        SimpleThread thread = new SimpleThread();
        //启动线程
        thread.start();
    }
}

使用Runnable接口

  • 创建一个类实现Runnable接口, 并重写该接口的run()方法,该run()方法的方法体同样是该线程的线程执行体。
  • 创建Runnable实现类的实例, 然后使用Runnable实例创建一个Thread实例,
  • 调用Thread实例的start方法开始线程
    在这里插入图片描述
package SimpleThread;

public class SimpleRunnable {
    
    
	
	//创建一个类实现Runnable接口
	public static class TaskOne implements Runnable{
    
    
		static int count = 0;
		//重写该接口的run()方法
		@Override
		public void run() {
    
    
			while(true) {
    
    
				count++;
				System.out.println("taskOne: " + count);
				try {
    
    
					Thread.sleep(500);
				} catch (InterruptedException e) {
    
    
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}

	}
	//创建一个类实现Runnable接口
	public static class TaskTwo implements Runnable{
    
    
		static int count = 0;
		//重写该接口的run()方法
		@Override
		public void run() {
    
    
			while(true) {
    
    
				count++;
				System.out.println("taskTwo: " + count);
				try {
    
    
					Thread.sleep(1000);
				} catch (InterruptedException e) {
    
    
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
	}
	
	public static void main(String[] args) {
    
    
		//创建Runnable实现类的实例
		TaskOne taskOne = new TaskOne();
		TaskTwo taskTwo = new TaskTwo();
		//使用Runnable实例创建Thread实例
		Thread t1 = new Thread(taskOne);
		Thread t2 = new Thread(taskTwo);
		//调用Thread实例的start方法开始线程
		t1.start();
		t2.start();
		
	}
}

使用Runnable接口和继承Thread类的区别 (continue)

使用Runnable接口

  • 优点
  • 使用Runnable接口的类还可以继承其他类
  • 多个线程可以共享一个target对象
  • 缺点
  • 编程稍微复杂, 比如要访问当前线程, 则必须使用Thread.currentThread()方法

继承Thread类

  • 优点

编写简单, 比如需要访问当前线程,则直接使用this

  • 缺点

因为已经继承了Thread类, 则不能继承其他父类

线程优先级 Thread Priorities

  • 每一个线程都有优先级
  • 一个线程默认继承父类线程的优先级, 但是也可以通过setPriority设置线程优先级. From MIN_PRIORITY (数值为1) to NORM_PRIORITY (数值为5)
  • 当scheduler选择线程时, 优先选择优先级高的线程.
  • 线程优先级根据系统不同也会有改变. Java虚拟机会根据host操作系统改变对应的系统优先级, 比如Windows有7个优先级, 有些Java的优先级会有一样的操作系统优先级. In the Oracle JVM for Linux, thread priorities are ignored altogether—all threads have the same priority
  • 如果有几个线程的优先级很高, 则低优先级的线程可能会处于饥饿状态
  • 所以不要将程序的正确性依赖于线程优先级.
    在这里插入图片描述

Daemon Thread

  • 当一个线程的唯一作用是服务于其他线程时, 比如timer thread给其他线程发送"timer ticks" , 这个线程被叫做Daemon Thread.
  • 可以通过t.setDaemon(true)将一个线程设置为Daemon Thread
  • 当只有daemon thread 存在时, JVM会自动退出.
  • 不要让Daemon Thread使用persistent resource, 比如数据或者文件. 因为daemon thread可能随时被终止.

猜你喜欢

转载自blog.csdn.net/weixin_38803409/article/details/125446068