Java常用工具_5.Java线程

一、线程

1.进程

进程是指可执行程序并存放在计算机存储器的一个指令序列,它是一个动态执行的过程
有些软件对应一个进程,有些软件由多个进程组成

2.什么是线程?

线程是比进程还要小的运行单位,一个进程包括多个线程
线程可以看作一个子程序

3.软件同时运行?

对于CPU来说,许多软件是轮流运行的,但是由于时间间隔非常短,所以用户会感觉软件是同时运行的

二、线程的创建

1.创建方式

(1)创建一个Thread类,或者一个Thread子类的对象
(2)创建一个实现Runnable接口的类的对象

2.Thread是一个线程类,位于java.lang包下

Thread构造方法
在这里插入图片描述
Thread类的常用方法
在这里插入图片描述

3.Runnable接口

(1)只有一个方法run();
(2)Runnable是Java中用以实现线程的接口
(3)任何实现线程功能的类都必须实现该接口

4.通过Thread类创建线程:重写run()方法

package com.study.thread;

class MyThread extends Thread {
	public void run() {
		System.out.println(getName() + "该线程正在执行!");
	}
}

public class ThreadTest {

	public static void main(String[] args) {
		System.out.println("主线程1");
		MyThread mt = new MyThread();
		mt.start();// 启动线程,会执行run()的代码
		// 线程是不能多次启动的,即只能调用一次start()方法
		System.out.println("主线程2");
//		主线程1
//		主线程2
//		Thread-0该线程正在执行!

	}

}

package com.study.thread1;

class MyThread extends Thread {
	public MyThread(String name) {
		super(name);
	}

	public void run() {
		for (int i = 0; i <= 10; i++) {
			System.out.println(getName() + "正在运行" + i);
		}
	}
}

public class ThreadTest {

	public static void main(String[] args) {
		MyThread mt1 = new MyThread("线程1");
		MyThread mt2 = new MyThread("线程2");
		mt1.start();
		mt2.start();
		// 线程获得cpu的使用权是随机的,所以运行顺序也是随机的
	}

}

5.通过实现Runnable接口的方式创建线程

为什么要实现Runnable接口?

  • Java不支持多继承
  • 不打算重写Thread类的其他方法
package com.study.runnable;

class PrintRunnable implements Runnable {
	int i = 1;// 多个线程处理同一个资源的情况

	@Override
	public void run() {
		while (i <= 10)
			System.out.println(Thread.currentThread().getName() + "正在运行" + (i++));
	}

}

public class Test {

	public static void main(String[] args) {
		PrintRunnable pr = new PrintRunnable();
		Thread t1 = new Thread(pr);
		t1.start();
		// PrintRunnable pr1 = new PrintRunnable();
		Thread t2 = new Thread(pr);
		t2.start();

	}

}

三、线程的状态和生命周期

1.线程的5个状态

(1)新建(New)
(2)可运行(Runnable)(也称就绪)
(3)正在运行(Running)
(4)阻塞(Blocked)
(5)终止(Dead)

2.线程的生命周期

在这里插入图片描述

四、sleep方法(Runnable→Blocked)

1.Thread类的方法

public static void sleep(long millis)

2.作用

在指定的毫秒数内让正在执行的线程休眠(暂停执行)

3.参数为休眠的时间,单位是毫秒

4.代码实现

package com.study.sleep;

class MyThread implements Runnable {

	@Override
	public void run() {
		for (int i = 1; i <= 30; i++) {
			System.out.println(Thread.currentThread().getName() + "执行第" + i + "次");
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}

public class SleepDemo {

	public static void main(String[] args) {
		MyThread mt = new MyThread();
		Thread t = new Thread(mt);
		t.start();
		Thread t1 = new Thread(mt);
		t1.start();

	}

}

五、join方法(Runnable→Blocked)

1.Thread类的方法

public final void join();

2.作用

等待调用该方法的线程结束后才能执行(抢占资源)

3.public final void join(long millins)

等待该线程终止的最长时间为millis毫秒.
如果millis为0则意味着要一直等下去.

4.代码实现

package com.study.join;

class MyThread extends Thread {
	public void run() {
		for (int i = 1; i <= 500; i++) {
			System.out.println(getName() + "正在执行" + i + "次!");
		}
	}
}

public class JoinDemo {

	public static void main(String[] args) {
		MyThread mt = new MyThread();
		mt.start();
		try {
			mt.join(2);// 等待该线程终止的最长时间为2ms
			// mt.join();// 抢占资源,其他线程会等待该方法的线程执行结束后才执行
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for (int i = 1; i <= 20; i++) {
			System.out.println("主线程运行第" + i + "次");
		}
		System.out.println("主线程运行结束!");
	}

}

六、线程优先级

1.线程的优先级

(1)Java为线程类提供了10个优先级
(2)优先级可以用整数1-10表示,超过范围会抛出异常
(3)主线程默认优先级为5

2.优先级常量

MAX_PRIORITY:线程的最高优先级10
MIN_PRIORITY:线程的最低优先级1
NORM_PRIORITY:线程的默认优先级5

3.优先级相关的方法

在这里插入图片描述

4.代码实现

package com.study.priority;

class MyThread extends Thread {
	private String name;

	public MyThread(String name) {
		this.name = name;
	}

	public void run() {
		for (int i = 1; i <= 50; i++) {
			System.out.println("线程" + name + "正在运行" + i);
		}
	}
}

public class PriorityDemo {

	public static void main(String[] args) {
		// 获取主线程的优先级
		int mainPriority = Thread.currentThread().getPriority();
		// System.out.println("主线程的优先级为:" + mainPriority);
		MyThread mt1 = new MyThread("线程1");
		MyThread mt2 = new MyThread("线程2");
		mt1.setPriority(10);// 设置线程1的优先级为10
		// mt1.setPriority(Thread.MAX_PRIORITY);
		mt2.setPriority(1);
		mt2.start();
		mt1.start();
		// System.out.println("线程1的优先级为" + mt1.getPriority());
	}

}

七、线程同步

1.多线程运行问题

• 各个线程是通过竞争CPU时间而获得运行机会的
• 各线程什么时候得到CPU时间,占用多久,是不可预测的
• 一个正在运行着的线程在什么地方被暂停是不确定的

2.银行存取款问题

为了保证在存款或取款的时候,不允许其他线程对帐户余额进行操作,需要将Bank对象进行锁定
使用关键字synchronized实现

3.同步

synchronized关键字用在 成员方法、静态方法、语句块

4.银行存取款系统代码实现

package com.study.bank;

public class Bank {
	private String account;// 账号
	private int balance;// 账户余额

	public Bank(String account, int balance) {
		this.account = account;
		this.balance = balance;
	}

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public int getBalance() {
		return balance;
	}

	public void setBalance(int balance) {
		this.balance = balance;
	}

	@Override
	public String toString() {
		return "Bank [账号:" + account + ", 余额:" + balance + "]";
	}

	// 存款
	public synchronized void saveAccount() {
		// 可以在不同的位置处添加sleep方法
		// 获取当前的账户余额
		int balance = getBalance();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 修改余额 ,存100元
		balance += 100;
		// 修改账户余额
		setBalance(balance);
		// 输出存款后账户余额
		System.out.println("存款100元后的账户余额:" + balance);
	}

	public void drawAccount() {
		synchronized (this) {
			// 在不同的位置添加sleep方法
			// 获取当前的账户余额
			int balance = getBalance();
			// 修改余额 ,取200
			balance = balance - 200;
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// 修改账户余额
			setBalance(balance);
			System.out.println("取款200后的账户余额:" + balance);
		}
	}
}

package com.study.bank;

//取款
public class DrawAccount implements Runnable {
	Bank bank;

	public DrawAccount(Bank bank) {
		this.bank = bank;
	}

	@Override
	public void run() {
		bank.drawAccount();
	}

}

package com.study.bank;

//存款
public class SaveAccount implements Runnable {
	Bank bank;

	public SaveAccount(Bank bank) {
		this.bank = bank;
	}

	public void run() {
		bank.saveAccount();
	}
}

package com.study.bank;

public class Test {

	public static void main(String[] args) {
		// 创建账户,余额为1000
		Bank bank = new Bank("1001", 1000);
		// 创建线程对象
		SaveAccount sa = new SaveAccount(bank);
		DrawAccount da = new DrawAccount(bank);
		Thread save = new Thread(sa);
		Thread draw = new Thread(da);
		save.start();
		draw.start();
		try {
			draw.join();
			save.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(bank);
	}

}

八、线程间通信

1.问题:帐户余额不够了怎么办?

等待存入足够的钱后处理

2.等待和唤醒方法

wait()方法:中断方法的执行,使线程等待
notify()方法:唤醒处于等待的某一个线程,使其结束等待
nofifyAll()方法:唤醒所有处于等待的线程,使它们结束等待

3.生产者消费者代码实现

package com.study.queue;

public class Queue {
	private int n;
	boolean flag = false;// false:Queue中没有数据,生产数据后flag变为true,此时可以消费

	public synchronized int get() {
		if (!flag) {
			try {
				wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println("消费:" + n);
		flag = false;// 消费完毕,容器中没有数据
		notifyAll();
		return n;
	}

	public synchronized void set(int n) {
		if (flag) {
			try {
				wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println("生产:" + n);
		this.n = n;
		flag = true;// 生产完毕,容器中已经有数据
		notifyAll();
	}
}

package com.study.queue;

public class Consumer implements Runnable {
	Queue queue;

	Consumer(Queue queue) {
		this.queue = queue;
	}

	@Override
	public void run() {
		while (true) {
			queue.get();
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

}

package com.study.queue;

public class Producer implements Runnable {
	Queue queue;

	Producer(Queue queue) {
		this.queue = queue;
	}

	@Override
	public void run() {
		int i = 0;
		while (true) {
			queue.set(i++);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

}

package com.study.queue;

public class Test {

	public static void main(String[] args) {
		Queue queue = new Queue();
		new Thread(new Producer(queue)).start();
		new Thread(new Consumer(queue)).start();
	}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值