Day25_多线程第二天
1、单例(掌握)
class Singleton {//1,私有构造方法,其他类不能访问该构造方法了private Singleton(){}//2,创建本类对象private static Singleton s = new Singleton();//3,对外提供公共的访问方法public static Singleton getInstance() {//获取实例return s;}}
class Singleton {//1,私有构造方法,其他类不能访问该构造方法了private Singleton(){}//2,声明一个引用private static Singleton s ;//3,对外提供公共的访问方法public static Singleton getInstance() {//获取实例if(s == null) {//线程1等待,线程2等待s = new Singleton();}return s;}}
2、JAVA通过命令执行Windows程序-Runtime
public static void main(String[] args) throws Exception{//执行windows中的某些命令Process exec = Runtime.getRuntime().exec("arp -a");//获取控制台打印出的数据BufferedReader stream = new BufferedReader(new InputStreamReader(exec.getInputStream()));String line;while((line=stream.readLine()) != null){System.out.println(line);}}
3、定时器-Timer
/** void schedule(TimerTask 要执行的任务, Date 首次运行时间, long 每隔多久执行一次)*/public static void main(String[] args) {//从当前时间开始执行,每隔1S执行一次new Timer().schedule(new TimerTask() {@Overridepublic void run() {System.out.println("HEH");}}, new Date(),1000);}
4、线程之间的通信(多个线程共享同一数据的问题)
package com.heima.thread2;public class Demo1_Notify {/*** @param args* 等待唤醒机制*/public static void main(String[] args) {final Printer p = new Printer();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();}}//等待唤醒机制class Printer {private int flag = 1;public void print1() throws InterruptedException {synchronized(this) {if(flag != 1) {this.wait(); //当前线程等待}System.out.print("黑");System.out.print("马");System.out.print("程");System.out.print("序");System.out.print("员");System.out.print("\r\n");flag = 2;this.notify(); //随机唤醒单个等待的线程}}public void print2() throws InterruptedException {synchronized(this) {if(flag != 2) {this.wait();}System.out.print("传");System.out.print("智");System.out.print("播");System.out.print("客");System.out.print("\r\n");flag = 1;this.notify();}}}
5、1.5新特性
package com.heima.thread2;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.ReentrantLock;public class Demo3_ReentrantLock {/*** @param args*/public static void main(String[] args) {final 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) {c1.await();}System.out.print("黑");System.out.print("马");System.out.print("程");System.out.print("序");System.out.print("员");System.out.print("\r\n");flag = 2;//this.notify(); //随机唤醒单个等待的线程c2.signal();r.unlock(); //释放锁}public void print2() throws InterruptedException {r.lock();if(flag != 2) {c2.await();}System.out.print("传");System.out.print("智");System.out.print("播");System.out.print("客");System.out.print("\r\n");flag = 3;//this.notify();c3.signal();r.unlock();}public void print3() throws InterruptedException {r.lock();if(flag != 3) {c3.await();}System.out.print("i");System.out.print("t");System.out.print("h");System.out.print("e");System.out.print("i");System.out.print("m");System.out.print("a");System.out.print("\r\n");flag = 1;c1.signal();r.unlock();}}
6、线程组(了解)
MyRunnable mr = new MyRunnable();Thread t1 = new Thread(mr, "张三");Thread t2 = new Thread(mr, "李四");//获取线程组// 线程类里面的方法:public final ThreadGroup getThreadGroup()ThreadGroup tg1 = t1.getThreadGroup();ThreadGroup tg2 = t2.getThreadGroup();// 线程组里面的方法:public final String getName()String name1 = tg1.getName();String name2 = tg2.getName();System.out.println(name1);System.out.println(name2);// 通过结果我们知道了:线程默认情况下属于main线程组// 通过下面的测试,你应该能够看到,默任情况下,所有的线程都属于同一个组System.out.println(Thread.currentThread().getThreadGroup().getName());自己设定线程组// ThreadGroup(String name)ThreadGroup tg = new ThreadGroup("这是一个新的组");MyRunnable mr = new MyRunnable();// Thread(ThreadGroup group, Runnable target, String name)Thread t1 = new Thread(tg, mr, "张三");Thread t2 = new Thread(tg, mr, "李四");System.out.println(t1.getThreadGroup().getName());System.out.println(t2.getThreadGroup().getName());//通过组名称设置后台线程,表示该组的线程都是后台线程tg.setDaemon(true);
7、线程池(了解)
// public static ExecutorService newFixedThreadPool(int nThreads)ExecutorService pool = Executors.newFixedThreadPool(2);// 可以执行Runnable对象或者Callable对象代表的线程pool.submit(new MyRunnable());pool.submit(new MyRunnable());//结束线程池pool.shutdown();
8、线程第三种创建方式(了解)
// 创建线程池对象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();public class MyCallable implements Callable<Integer> {private int number;public MyCallable(int number) {this.number = number;}@Overridepublic Integer call() throws Exception {int sum = 0;for (int x = 1; x <= number; x++) {sum += x;}return sum;}}
5、等待唤醒机制
1、前提(掌握)
wait:是Object类的方法,可以不用传递参数,释放锁对象sleep:是Thread类的静态方法,需要传递参数,不释放所对象
public synchronized void set(String name, int age) {if (this.flag) {try {Object.class.wait();} catch (Exception e) {}}this.name = name;this.age = age;this.flag = true;Object.class.notify();}
6、工厂模式(掌握)
7、适配器模式(掌握)
package com.heima.适配器;public class Demo1_Adapter {/*** @param args* 适配器设计模式* 鲁智深*/public static void main(String[] args) {}}interface 和尚 {public void 打坐();public void 念经();public void 撞钟();public void 习武();}abstract class 天罡星 implements 和尚 { //声明成抽象的原因是,不想让其他类创建本类对象,因为创建也没有意义,方法都是空的@Overridepublic void 打坐() {}@Overridepublic void 念经() {}@Overridepublic void 撞钟() {}@Overridepublic void 习武() {}}class 鲁智深 extends 天罡星 {public void 习武() {System.out.println("倒拔垂杨柳");System.out.println("拳打镇关西");System.out.println("大闹野猪林");System.out.println("......");}}
13、今天必须掌握的内容,面试题,笔试题。(掌握这个就可以放心学习后面的知识了)
Day25_多线程第二天的更多相关文章
- “全栈2019”Java多线程第二十九章:可重入锁与不可重入锁详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- “全栈2019”Java多线程第二十八章:公平锁与非公平锁详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- “全栈2019”Java多线程第二十七章:Lock获取lock/释放unlock锁
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- “全栈2019”Java多线程第二十六章:同步方法生产者与消费者线程
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- “全栈2019”Java多线程第二十五章:生产者与消费者线程详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- “全栈2019”Java多线程第二十四章:等待唤醒机制详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- “全栈2019”Java多线程第二十三章:活锁(Livelock)详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- “全栈2019”Java多线程第二十二章:饥饿线程(Starvation)详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- “全栈2019”Java多线程第二十一章:同步代码块产生死锁的例子
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
随机推荐
- VC环境配置办法
在VS工程中,添加c/c++工程中外部头文件及库的基本步骤:1.添加工程的头文件目录:工程---属性---配置属性---c/c++---常规---附加包含目录:加上头文件存放目录.2.添加文件引用的l ...
- session_id 恢复 session的内容
php的session是可以程序恢复的,这个和java不太一样.session的恢复机制可以实现多个应用程序session的共享,因为php的session都是以文件形式或者数据库存储的.首先是ses ...
- 多个radiobutton选定一个
asp.net中怎么判断其中一个radiobutton被选中后登录的是一个窗体,另一个被选中后登录的是另一个窗体. 页面设置两按钮的GroupName为同一组值: <asp:RadioButto ...
- RegExp
var str = "1a1b1c";var reg = new RegExp("1(.)", "g");alert(reg.test(st ...
- Bootstrap<基础十四> 按钮下拉菜单
使用 Bootstrap class 向按钮添加下拉菜单.如需向按钮添加下拉菜单,只需要简单地在在一个 .btn-group 中放置按钮和下拉菜单即可.也可以使用 <span class=&qu ...
- spring事务详解(转载+高亮)
spring提供的事务管理可以分为两类:编程式的和声明式的.编程式的,比较灵活,但是代码量大,存在重复的代码比较多:声明式的比编程式的更灵活.编程式主要使用transactionTemplate.省略 ...
- UDPClient的用法
UDP_Server: UdpClient receivingUdpClient = ); IPEndPoint RemoteIpEndPoint = ); try { byte[] sdata = ...
- Delphi: 有关Form处理 :需要调用的时候进行调用。
if not Assigned(frmAppServer) then frmAppServer := TfrmAppServer.Create(Application); frmAppServer.S ...
- MFC-01-Chapter01:Hello,MFC---1.3 第一个MFC程序(01)
#include <afxwin.h> class CMyApp : public CWinApp { public: virtual BOOL InitInstance(); }; cl ...
- Console命令详解,让调试js代码变得更简单
Firebug是网页开发的利器,能够极大地提升工作效率. 但是,它不太容易上手.我曾经翻译过一篇<Firebug入门指南>,介绍了一些基本用法.今天,继续介绍它的高级用法. ======= ...