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多 ...
随机推荐
- lamp遇到问题
1.配置好apache和虚拟host,显示无权, 将网站目录更新755,还是不行,最后selinux: 1.设置下面即可: chcon -R -h -t httpd_sys_content_t /ho ...
- 腾讯的一道JavaScript面试题
//题目:分别弹出什么内容? <!-- function test(){ this.a = 1; alert(this); //[object Window] } test(); var t = ...
- CAST()函数
语法: CAST(expression AS data_type) 参数说明: expression:任何有效的SQServer表达式 AS:用于分割两个参数,在AS之前的是需要处理的数据,在AS之后 ...
- ios 重签名
http://stackoverflow.com/questions/6569120/resigning-an-application-outside-xcode #!/bin/sh TEMPDIR= ...
- VBA唏嘘戏——简单单元格的设定(实例)
由于有很多个Word文件,所以应用宏会更加方便排版,而且版式较为统一. Sub 设置列宽() ' ' 设置列宽宏 ' ' ActiveDocument.Tables().Cell(, ).Width ...
- Rhel6-csync配置文档
系统环境: rhel6 x86_64 iptables and selinux disabled 主机:192.168.122.160 server60.example.com 192.168.122 ...
- [导读]Learning from Imbalanced Classes
原文:Learning from Imbalanced Classes 数据不平衡是一个非常经典的问题,数据挖掘.计算广告.NLP等工作经常遇到.该文总结了可能有效的方法,值得参考: Do nothi ...
- c++ 数据类型转换: static_cast dynamic_cast reinterpret_cast const_cast
c++ 数据类型转换: static_cast dynamic_cast reinterpret_cast const_cast [版权声明]转载请注明出处 http://www.cnblogs.c ...
- spark应用程序常见问题整理
1.executor lost /java oom 通常是由于单个task内存占用过多,可以观察是哪个阶段挂的,如果类似groupbykey,可以看看是否有数据倾斜现象 如果不是,可以repartit ...
- LR一个简单的流程
1.录制脚本 2.回放脚本 :回放前的运行时设置:run_time_seting F4 关联设置(动态值) 日志分析 3.脚本的增强: 添加事物(计时) 参数化(模拟真实的用户行为) 内容检查.回 ...