38.多线程

多线程

1.线程间的通信
1.1.两个线程间的通信

1.什么时候需要通信

  • 多个线程并发执行时, 在默认情况下CPU是随机切换线程的
  • 如果我们希望他们有规律的执行, 就可以使用通信, 例如每个线程执行一次打印

2.怎么通信

  • 如果希望线程等待, 就调用wait()
  • 如果希望唤醒等待的线程, 就调用notify();
  • 这两个方法必须在同步代码中执行, 并且使用同步锁对象来调用
1.2.三个或三个以上间的线程通信

多个线程通信的问题

  • notify()方法是随机唤醒一个线程
  • notifyAll()方法是唤醒所有线程
  • JDK5之前无法唤醒指定的一个线程
  • 如果多个线程之间通信, 需要使用notifyAll()通知所有线程, 用while来反复判断条件
package heima_day25;
public class Demo5_NotifyAll {
     public static void main(String[] args) {
            final Printer2 p = new Printer2();
           new Thread() {
                public void run() {
                     while(true) {
                           try {
                              p.print1();
                           } catch (InterruptedException e) {
                                e.printStackTrace();
                           }
                     }
                }
           }.start();
           new Thread() {
                public void run() {
                     while(true) {
                           try {
                                p.print2();
                           } catch (InterruptedException e) {
                                e.printStackTrace();
                           }
                     }
                }
           }.start();        
           new Thread() {
                public void run() {
                     while(true) {
                           try {
                                p.print3();
                           } catch (InterruptedException e) {
                                e.printStackTrace();
                          }
                     }
                }
           }.start();
     }
}

/*

 * 1,在同步代码块中,用哪个对象锁,就用哪个对象调用wait方法
 * 2,为什么wait方法和notify方法定义在Object这个类中?
 * 因为锁对象可以是任意对象,object是所有的类的基类,所有wait方法和notify方法需要定义在objetc这个类中
 * 3,sleep方法和wait方法的区别
 * a,sleep方法必须传入参数,参数就是时间,时间到了自动醒来
 * wait方法可以传入参数也可以不传入参数,传入参数就是在参数的时间结束后等待,不传入参数就是直接等待
 * b,sleep方法在同步函数或同步代码块中,不释放锁,睡着了也抱着锁睡
 * wait方法在同步函数或同步代码块中,释放锁
 */
class Printer2{
     private int flag = 1;
     public  void print1() throws InterruptedException {
           synchronized(this) {
                //if(flag != 1) 
                while(flag != 1){
                     this.wait();
                }
                System.out.print("a");
                System.out.print("b");
                System.out.print("c");
                System.out.println();
                flag = 2;
                //this.notify();
                this.notifyAll();
           }
     }
     public void print2() throws InterruptedException {
           synchronized (this) {
                //if(flag != 2) 
                while(flag != 2){
                     this.wait();
                }
                System.out.print("d");
                System.out.print("e");
                System.out.print("f");
                System.out.println();
                flag = 3;
                //this.notify();
                this.notifyAll();
           }
     }    
           public void print3() throws InterruptedException {
                synchronized (this) {
                     //if(flag != 3)//if语句是在哪等待,在哪起来
                           while(flag != 3){
                           this.wait();
                     }
                     System.out.print("h");
                     System.out.print("i");
                     System.out.print("j");
                     System.out.println();
                     flag = 1;
                     //this.notify();
                     this.notifyAll();
                }
     }
}
1.3.JDK1.5的新特性互斥锁

1.同步

  • 使用ReentrantLock类的lock()和unlock()方法进行同步
    2.通信
  • 使用ReentrantLock类的newCondition()方法可以获取Condition对象
  • 需要等待的时候使用Condition的await()方法, 唤醒的时候用signal()方法
  • 不同的线程使用不同的Condition, 这样就能区分唤醒的时候找哪个线程了
package heima_day25;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class Demo6_syn_JDK {

	public static void main(String[] args) {
		Printer3 p = new Printer3();
		
		new Thread() {
			public void run() {
				while(true) {
					try {
						p.print1();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}.start();

		new Thread() {
			public void run() {
				while(true) {
					try {
						p.print2();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}.start();
		
		new Thread() {
			public void run() {
				while(true) {
					try {
						p.print3();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}.start();
	}

}
class Printer3{
	private ReentrantLock r = new ReentrantLock();
	private Condition c1 = r.newCondition();
	private Condition c2 = r.newCondition();
	private Condition c3 = r.newCondition();
	private int flag = 1;
	public  void print1() throws InterruptedException {
			r.lock();//获取锁
			//if(flag != 1) 
			if(flag != 1){
				c1.await();
			}
			System.out.print("a");
			System.out.print("b");
			System.out.print("c");
			System.out.println();
			flag = 2;
			//this.notify();
			c2.signal();
			r.unlock();//释放锁
		
	}
	public void print2() throws InterruptedException {
		r.lock();
			//if(flag != 2) 
			if(flag != 2){
				c2.await();
			}
			System.out.print("d");
			System.out.print("e");
			System.out.print("f");
			System.out.println();
			flag = 3;
			//this.notify();
			c3.signal();
			r.unlock();
	}
		
		public void print3() throws InterruptedException {
				r.lock();
				//if(flag != 3)//if语句是在哪等待,在哪起来
					if(flag != 3){
					c3.await();
				}
				System.out.print("h");
				System.out.print("i");
				System.out.print("j");
				System.out.println();
				flag = 1;
				//this.notify();
				c1.signal();
				r.unlock();

	}
}
2.线程组的概述和使用

线程组概述

  • Java中使用ThreadGroup来表示线程组,它可以对一批线程进行分类管理,Java允许程序直接对线程组进行控制。

默认情况下,所有的线程都属于主线程组。

  • public final ThreadGroup getThreadGroup()//通过线程对象获取他所属于的组
  • public final String getName()//通过线程组对象获取他组的名字

我们也可以给线程设置分组

  • 1,ThreadGroup(String name) 创建线程组对象并给其赋值名字
  • 2,创建线程对象
  • 3,Thread(ThreadGroup?group, Runnable?target, String?name)
  • 4,设置整组的优先级或者守护线程

案例演示:

package heima_day25;
public class Demo7_ThreadGroup {
     public static void main(String[] args) {
           //Demo1();
           ThreadGroup tg = new ThreadGroup("我是一个新的线程组");//创建新的线程组
           MyRunnable mr =new MyRunnable();//创建Runnable的子类对象    
           Thread t1 = new Thread(tg,mr,"张三");//将线程t1放在组中
           Thread t2 = new Thread(tg,mr,"李四");//将线程t2放在组中         
           System.out.println(t1.getThreadGroup().getName());//获取组名
           System.out.println(t2.getThreadGroup().getName());
           //通过组名称设置后台线程,表示该组的线程都是后台线程
           tg.setDaemon(true);
     }
     private static void Demo1() {
           MyRunnable mr =new MyRunnable();
           Thread t1 = new Thread(mr, "张三");
           Thread t2 = new Thread(mr, "李四");         
           ThreadGroup tg1 = t1.getThreadGroup();
           ThreadGroup tg2 = t2.getThreadGroup();
            System.out.println(tg1.getName());//默认是主线程
           System.out.println(tg2.getName());
     }
}
class MyRunnable implements Runnable{
     @Override
     public void run() {
           for(int i = 0; i < 1000; i++) {
                System.out.println(Thread.currentThread().getName()+"----"+i);
           }
     } 
}


3.线程的五种状态

线程分为5个生命周期,新建,就绪,运行,阻塞,死亡

其中:

新建代表线程在内存中创建,对应start方法。

就绪代表线程拥有抢夺执行权的资格,如果抢到就会执行线程中的内容

运行代码线程中的内容正在执行。

a:若被抢走执行权,回到就绪状态

b:若执行ssleep、wait等方法,会进入阻塞状态。

阻塞代表线程被强制不可进入就绪状态,对于非就绪状态的线程是没有机会抢夺执行权,也就更不可能进入运行状态了。

死亡代表线程运行结束,也可能是被强制结束,一般不建议使用。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pc5o3mDj-1570948811740)(en-resource://database/757:1)]

4.线程池的概述和使用

线程池概述

  • 程序启动一个新线程成本是比较高的,因为它涉及到要与操作系统进行交互。而使用线程池可以很好的提高性能,尤其是当程序中要创建大量生存期很短的线程时,更应该考虑使用线程池。线程池里的每一个线程代码结束后,并不会死亡,而是再次回到线程池中成为空闲状态,等待下一个对象来使用。在JDK5之前,我们必须手动实现自己的线程池,从JDK5开始,Java内置支持线程池

内置线程池的使用概述

  • JDK5新增了一个Executors工厂类来产生线程池,有如下几个方法
  • public static ExecutorService newFixedThreadPool(int nThreads)
  • public static ExecutorService newSingleThreadExecutor()
  • 这些方法的返回值是ExecutorService对象,该对象表示一个线程池,可以执行Runnable对象或者Callable对象代表的线程。
  • 它提供了如下方法:
    uture<?> submit(Runnable task)
    Future submit(Callable task)

使用步骤:
1、创建线程池对象
2、创建Runnable实例
3、提交Runnable实例
4、关闭线程池

package heima_day25;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Demo8_ThreadPool {
	/**
	 * public static ExecutorService newFixedThreadPool(int nThreads)
	 * public static ExecutorService newSingleThreadExecutor()
	 */
	public static void main(String[] args) {
		//创建线程池
		ExecutorService pool = Executors.newFixedThreadPool(2);
		
		// 将线程放进池子里,并执行。可以执行Runnable对象或者Callable对象代表的线程
		pool.submit(new MyRunnable());
		pool.submit(new MyRunnable());

		//结束线程池
		pool.shutdown();
	}

}

5.多线程程序实现的方式
package heima_day25;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Demo9_Callable {
	/**
	 * 提交的是Callable
	 * @param args
	 * @throws ExecutionException 
	 * @throws InterruptedException 
	 * 	
	 * 多线程程序实现的方式3
	 * * 好处:
		* 可以有返回值
		* 可以抛出异常
		
	* 弊端:
		* 代码比较复杂,所以一般不用
	 */
	public static void main(String[] args) throws InterruptedException, ExecutionException {
		// TODO Auto-generated method stub
		// 创建线程池对象
		ExecutorService pool = Executors.newFixedThreadPool(2);

		// 可以执行Runnable对象或者Callable对象代表的线程
		Future<Integer> f1 = pool.submit(new MyCallable(100));
		Future<Integer> f2 = pool.submit(new MyCallable(200));

		// V get()
		Integer i1 = f1.get();
		Integer i2 = f2.get();

		System.out.println(i1);
		System.out.println(i2);

		// 结束
		pool.shutdown();


	}

}
		class MyCallable implements Callable<Integer> {

			private int number;
		
			public MyCallable(int number) {
				this.number = number;
			}
		
			public Integer call() throws Exception {
				int sum = 0;
				for (int x = 1; x <= number; x++) {
					sum += x;
				}
				return sum;
			}
		
		}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园建设方案旨在通过融合先进技术,如物联网、大数据、人工智能等,实现校园的智能化管理与服务。政策的推动和技术的成熟为智慧校园的发展提供了基础。该方案强调了数据的重要性,提出通过数据的整合、开放和共享,构建产学研资用联动的服务体系,以促进校园的精细化治理。 智慧校园的核心建设任务包括数据标准体系和应用标准体系的建设,以及信息化安全与等级保护的实施。方案提出了一站式服务大厅和移动校园的概念,通过整合校内外资源,实现资源共享平台和产教融合就业平台的建设。此外,校园大脑的构建是实现智慧校园的关键,它涉及到数据中心化、数据资产化和数据业务化,以数据驱动业务自动化和智能化。 技术应用方面,方案提出了物联网平台、5G网络、人工智能平台等新技术的融合应用,以打造多场景融合的智慧校园大脑。这包括智慧教室、智慧实验室、智慧图书馆、智慧党建等多领域的智能化应用,旨在提升教学、科研、管理和服务的效率和质量。 在实施层面,智慧校园建设需要统筹规划和分步实施,确保项目的可行性和有效性。方案提出了主题梳理、场景梳理和数据梳理的方法,以及现有技术支持和项目分级的考虑,以指导智慧校园的建设。 最后,智慧校园建设的成功依赖于开放、协同和融合的组织建设。通过战略咨询、分步实施、生态建设和短板补充,可以构建符合学校特色的生态链,实现智慧校园的长远发展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值