java线程简析

来源:互联网 发布:中国国家数据统计网 编辑:程序博客网 时间:2024/05/22 07:47

线程简析:

线程是正在运行程序进程中单一的执行流,一个进程对应一个程序,一个程序可以有多个进程,进程有自己的独立空间,线程只能共享进程中的空间,一个进程至少包含一个线程。线程最大的特点是可以同时并发执行多个任务。

比如程序要执行煮饭、洗菜、切菜、炒菜、吃饭五个方法,煮饭要20分钟,洗菜15分钟,切菜5分钟,炒菜15分钟,最后吃饭。在不使用线程时,程序执行这五个方法是窜起来,一个方法接一个方法去完成,总共要花20+15+5+15=55分钟,而使用线程时,程序的运行是这样的:在煮饭的时候CPU会有休眠的时间,这时CPU就会执行下一个方法即洗菜,洗菜的时候可能在接水时需要等待,那么CPU右可以执行下一个方法,按照这样的逻辑执行,这时程序的执行这五个方法就像人执行煮饭、洗菜、切菜、炒菜、吃饭这五个过程一样,合理和最大效率的完成这五件事情,也就是说使CPU的闲置时间减少,从而提高效率,节省时间。

 

实现多线程的方法:

继承Thread

<pre name="code" class="java">public class MyThread extends Thread{//重写run方法public void run(){//要做的事}}实现Runnable接口public class MyThread implements Runnable{//重写run方法public void run(){//要做的事}}下面用具体代码体现:public class Man {public static void main(String[] args) {Women men = new Women();//煮饭:启动煮饭线程CookieThread ct = new CookieThread();ct.start();//启动线程//洗菜WashThread wt = new WashThread();wt.start();//切菜CutThread cut = new CutThread();//创建线程对象Thread cutThread = new Thread(cut);//把线程对象包装成Thread对象cutThread.start();//炒菜FireThread fire = new FireThread();//创建线程对象Thread fireThread = new Thread(fire);//把线程对象包装成Thread对象fireThread.start();//吃饭men.eat();}public void eat(){System.out.println("开始吃饭了!");}}//煮饭类的线程public class CookieThread extends Thread{public void run() {cookie();}/** * 煮饭 */public void cookie(){System.out.println("开始煮饭.....");try {//休眠2秒Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("煮饭完成!");}}//洗菜类的线程public class WashThread extends Thread{public void run() {wash();}/** * 洗菜 */public void wash(){System.out.println("开始洗菜.....");try {//休眠2秒Thread.sleep(1500);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("洗菜完成!");}}//切菜类的线程public class CutThread implements Runnable{public void run() {cut();}/** * 切菜 */public void cut(){System.out.println("开始切菜.....");try {//休眠2秒Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("切菜完成!");}}//炒菜类的线程public class FireThread implements Runnable{public void run() {fire();}/** * 炒菜 */public void fire(){System.out.println("开始炒菜.....");try {//休眠2秒Thread.sleep(1500);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("炒菜完成!");}}


Synchronized同步锁关键字、wait()方法、notify()方法:

用生产/消费模型说明:

public class Producter extends Thread{private ArrayList<Iphone> list;public Producter(ArrayList<Iphone> list){this.list = list;}public void run() {//定义一个计数器,来标志手机序列号int count = 0;synchronized (list) {while(true){//判断当前集合中是否还有手机if(list.size()>0){//说明集合中还有手机,不应该生产try {list.wait();} catch (InterruptedException e) {e.printStackTrace();}}//生产手机Iphone ip = new Iphone("苹果5S"+count);count++;list.add(ip);System.out.println("生产商生产了一个手机,手机型号为:"+ip.name);//通知消费者,不要再等待了,可以消费手机list.notify();}}}}



Synchronized同步锁关键字、wait()方法、notify()方法:

用生产/消费模型说明:

public class Producter extends Thread{private ArrayList<Iphone> list;public Producter(ArrayList<Iphone> list){this.list = list;}public void run() {//定义一个计数器,来标志手机序列号int count = 0;synchronized (list) {while(true){//判断当前集合中是否还有手机if(list.size()>0){//说明集合中还有手机,不应该生产try {list.wait();} catch (InterruptedException e) {e.printStackTrace();}}//生产手机Iphone ip = new Iphone("苹果5S"+count);count++;list.add(ip);System.out.println("生产商生产了一个手机,手机型号为:"+ip.name);//通知消费者,不要再等待了,可以消费手机list.notify();}}}}public class Customer extends Thread{private ArrayList<Iphone> list;public Customer(ArrayList<Iphone> list){this.list = list;}public void run() {super.run();synchronized (list) {while(true){//访问仓库中的数据是否还有if(list.size()==0){//处于等待try {list.wait();//等待} catch (InterruptedException e) {e.printStackTrace();}}//买手机Iphone ip = list.remove(0);//输出手机的型号System.out.println("买了一个手机,型号为:"+ip.name);//告诉生产者,你可以生产手机list.notify();//通知}}}}public class Iphone {//定义属性public String name;public Iphone(String name){this.name = name;}}public class Test {public static void main(String[] args) {//定义一个集合仓库,用来存储共同访问的手机ArrayList<Iphone> list = new ArrayList<Iphone>();//创建生产线程Producter product = new Producter(list);//创建消费线程Customer customer = new Customer(list);//运行线程product.start();customer.start();}}
运行结果为:

生产商生产了一个手机,手机型号为:苹果5S84620
买了一个手机,型号为:苹果5S84620
生产商生产了一个手机,手机型号为:苹果5S84621
买了一个手机,型号为:苹果5S84621
生产商生产了一个手机,手机型号为:苹果5S84622
买了一个手机,型号为:苹果5S84622
。。。。。。。。。


线程池:

线程的生命周期:

线程状态:


比如现在公司有五名员工,公司有一个任务列表,需要十名员工一起完成所有任务:

这五名员工是每个人随机获得一个任务,而且每名员工几乎是同时去获取完成任务

public class ThreadPool {//先定义一个容器,用来存储执行线程private PoolWorker[] workerArray = new PoolWorker[10];//定义一个容器,用来存储任务private ArrayList<MyTask> taskList = new ArrayList<MyTask>();public ThreadPool(){//预置好所有的执行线程for(int i=0;i<workerArray.length;i++){PoolWorker worker = new PoolWorker(taskList);worker.setName("第"+i+"号员工");workerArray[i] = worker;worker.start();//启动线程System.out.println(worker.getName()+"已经准备完毕,等待执行任务!");}System.out.println("******************线程池预置完毕**************************");}//分配任务到任务列表public void excute(MyTask task){synchronized (taskList) {taskList.add(task);//通知所有等待的线程,可以取任务了taskList.notify();}}public static void main(String[] args) {//1.先创建公司(线程池)ThreadPool pool = new ThreadPool();//分配任务for(int i=0;i<60;i++){MyTask task = new MyTask(i);pool.excute(task);}}}public class PoolWorker extends Thread{//所有工作线程共享同一份任务列表private ArrayList<MyTask> taskList;public PoolWorker(ArrayList<MyTask> taskList){this.taskList = taskList;}@Overridepublic void run() {while(true){//同步关键字synchronized (taskList) {if(taskList.isEmpty()){try {taskList.wait();} catch (InterruptedException e) {e.printStackTrace();}}}//取出一个任务,并执行MyTask task = taskList.remove(0);System.out.println(this.getName()+"开始执行"+task.index+"任务");//执行任务task.work();}}}public class MyTask {//任务编号public int index;public MyTask(int index){this.index = index;}public void work(){System.out.println(index+"号任务开始....");try {Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(index+"号任务结束!");}}
输出结果为:

第0号员工已经准备完毕,等待执行任务!
第1号员工已经准备完毕,等待执行任务!
第2号员工已经准备完毕,等待执行任务!
第3号员工已经准备完毕,等待执行任务!
第4号员工已经准备完毕,等待执行任务!
******************线程池预置完毕**************************
第0号员工开始执行1任务
第3号员工开始执行3任务
第2号员工开始执行2任务
2号任务开始....
第4号员工开始执行1任务
1号任务开始....
3号任务开始....
1号任务开始....
第1号员工开始执行5任务
5号任务开始....
2号任务结束!
1号任务结束!
第2号员工开始执行5任务
5号任务开始....
3号任务结束!
5号任务结束!
1号任务结束!
5号任务结束!

0 0
原创粉丝点击