Java多线程-1(基础)

1.线程的创建

1. java创建线程有两种方式:

a.继承Thread类,重写run方法
b.继承Runnable接口,重写run方法
例如:
import java.lang.*;

public class ThreadPractice{
	public static void main(String[] args) {
		System.out.println("main thread start");
		Thread t1 = new Thread1();	//方式1创建Thread对象
		Thread t2 = new Thread(new Thread2());	//方式2创建Thread对象
		t1.start();
		t2.start();
		System.out.println("main thread end");
	}
}
//方式一:继承Thread类,重写run方法
class Thread1 extends Thread{
	@Override
	public void run() {
		System.out.println("Thread1 start");
		System.out.println("Thread1 end");
	}
}
//方式2:继承Runnable接口,重写run方法
class Thread2 implements Runnable{
	@Override
	public void run() {
		System.out.println("Thread2 start");
		System.out.println("Thread2 end");
	}
}

2. 线程中断(即结束某个线程的运行)

调用线程对象的interrupt方法,线程的run方法通过isInterrupt()函数来判断是否自己的Interrupt方法被调用,然后采用合适的方式结束线程的运行(类似于判断标志位)
例如:

import java.lang.*;

public class ThreadPractice{
	public static void main(String[] args) {
		System.out.println("main thread start");
		Thread t1 = new MyThread();
		t1.start();
		try {
			Thread.sleep(10);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		t1.interrupt();	//调用interrupt函数来中断t1线程
		System.out.println("main thread end");
	}
}

class MyThread extends Thread{
	@Override
	public void run() {
		int num = 0;
		//判断interrupt函数是否被调用
		while(!isInterrupted()) {
			System.out.println("num = " + num);
			num++;
		}
		System.out.println("MyThread end");
	}
}

注意:对一个处于等待状态的线程调用interrupt方法会触发InterruptedException异常。如子线程在sleep中,主线程调用该线程的interrupt方法,会触发InterruptedException异常,故在子线程调用sleep方法时,需要使用try-catch来捕获InterruptedException异常。

3. 守护线程

  1. 在java多线程程序中,主线程结束并不意味着整个进程结束,只要有一个线程在运行,整个进程就不会结束,守护线程除外。即:除守护线程外,只要有一个线程在运行,整个进程就不会退出。
  2. 守护线程一般扮演服务其它线程的角色,其自身一般处于无限循环中。
  3. 将一个线程设置为守护线程的方法:
    在启动线程之前,调用setDaemon(true)方法,将该线程设置为守护线程。
    如:
Thread t = new MyThread();
t.setDaemon(true);
t.start();
  1. 需要注意的是:守护线程不能持有需要关闭的资源(如打开文件等)。
  2. 守护线程使用例子
import java.lang.*;
import java.util.*;

public class ThreadPractice{
	public static void main(String[] args) {
		System.out.println("main thread start");
		Thread t1 = new MyThread();
		t1.setDaemon(true);	//设置为守护线程
		t1.start();
		try {
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("main thread end");
	}
}

class MyThread extends Thread{
	@Override
	public void run() {
		while(true) {
			Date date = new Date();
			System.out.println(date.toString());
			try {
				sleep(1000);	//调用sleep方法时,需要处理InterruptedException异常
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值