线程

与其战胜敌人一万次,不如战胜自己一次。


本讲内容:线程


一、什么是线程?(譬如:打开一个迅雷进程,可以有多个文件同时下载(即多个线程运行))

1、线程是轻量级的进程。

2、线程没有独立的地址空间(内存空间)

3、线程是由进程创建的(寄生在进程)

4、一个进程可以拥有多个线程(这就是我们常说的多线程编程)

5、线程有五种状态:

新建状态(new)、就绪状态(Runnable)、运行状态(Running)、阻塞状态(Blocked)、死亡状态(Dead)。



二、如何使用线程

在java中一个类要当作线程来使用有两种方法

1、继承Thread类、并重写run方法。

2、实现Runnable接口,并重写run方法。

两种方式比较:

Runnable方式可以避免Thread方式由于Java单继承性带来的缺陷
Runnable的代码可以被多个线程共享,适合于多个线程处理同一资源的情况(如共享总票数)。


三、两种方法的区别

本质上没有区别,从jdk帮助文档我们可以看到Thread类本身就实现了Runnable接口。

1、尽可能使用实现Runnable接口的方式来创建线程。

2、启动线程方式不同。


四、注意:不管是通过继承还是通过实现接口创建线程,它们的一个对象只能启动(即:start())一次,否则就会有异常抛出。


下面我们通过例子分别用二种方法实现:

示例一:每隔一秒,在控制台输出“hello,world”,当输出10次后,自动退出

1、继承Thread类、并重写run方法。

public class Text{
	public static void main(String[] args) {
		Student stu=new Student();
		stu.start();//启东线程   会导致run函数的运行
	}
}

class Student extends Thread{
	int times=0;
	public void run() {//重写run 函数
		while(true){
			try {//休眠一秒   以毫秒为单位
				// sleep就会让线程进入到阻塞状态,并释放资源
				//这个线程会自动休息一秒休息过后 线程会自动运行
				Thread.sleep(1000);
			} catch (Exception e) {
				// TODO: handle exception
			}
			times++;
			System.out.println("hello,world"+times);
			if(times==10){
				break;
			}
		}
	}
}

2、实现Runnable接口,并重写run方法。

public class Text{
	public static void main(String[] args) {
		Student stu=new Student();
		//创建一个线程对象
		Thread t=new Thread(stu);
		// 必须定义个一线程对象 然后把 实现 Runnable接口的对象包括里面
		t.start();
	}
}

class Student implements Runnable{
	int times=0;
	public void run() {//重写run 函数
		while(true){
			try {
				Thread.sleep(1000);
			} catch (Exception e) {
				// TODO: handle exception
			}
			times++;
			System.out.println("hello,world"+times);
			if(times==10){
				break;
			}
		}
	}
}


示例二:多线程实例(说明线程不同步)

public class Text{
	public static void main(String[] args) {
		Student stu=new Student();
		Dog dog=new Dog();
		Thread t1=new Thread(stu);
		Thread t2=new Thread(dog);
		t1.start();
		t2.start();
	}
}

class Student implements Runnable{
	int times=0;
	public void run() {//重写run 函数
		while(true){
			try {
				Thread.sleep(10);//阻塞0.01秒为了方便比较结果
			} catch (Exception e) {
				// TODO: handle exception
			}
			times++;
			System.out.println("人类线程在运行"+times);
			if(times==10){
				break;
			}
		}
	}
}

class Dog implements Runnable{
	int times=0;
	public void run() {//重写run 函数
		while(true){
			try {
				Thread.sleep(10);
			} catch (Exception e) {
				// TODO: handle exception
			}
			times++;
			System.out.println("动物线程在运行中……"+times);
			if(times==10){
				break;
			}
		}
	}
}

动物线程在运行中……1
人类线程在运行1
动物线程在运行中……2
人类线程在运行2
动物线程在运行中……3
人类线程在运行3
动物线程在运行中……4
<span style="color:#ff0000;">人类线程在运行4
人类线程在运行5</span>
<span style="color:#ff0000;">动物线程在运行中……5
动物线程在运行中……6</span>
人类线程在运行6
动物线程在运行中……7
人类线程在运行7
动物线程在运行中……8
人类线程在运行8
动物线程在运行中……9
<span style="color:#ff0000;">人类线程在运行9
人类线程在运行10</span>
动物线程在运行中……10


本讲就到这里,Take your time and enjoy it

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值