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多 ...
随机推荐
- JavaWeb chapter10 JavaWeb开发模式
1. 开发模式 (1)开发模式1:JSP+JavaBean (2)开发模式2:Servlet+JSP+JavaBean (MVC) 2.JavaBean 本质上是一个普通的Java类:需要遵循一定的 ...
- SQL Server中的日期格式化
SQL Server中文版的默认的日期字段datetime格式是yyyy-mm-dd Thh:mm:ss.mmm 例如: select getdate() 2004-09-12 11:06:08 ...
- ubuntu ulimit 设置
永久设置ubuntu ulimit 之前是ulimit -n 65535那样设置,不过貌似只是当前环境有效果,重启服务器的话,又失效了...今天无意找到一个设置的方法,可以永久设置ulimit的参数. ...
- E. Vasya and Beautiful Arrays
http://codeforces.com/contest/355/problem/E 每个数都可以变成段 [a-k,a], 某一个因子是否被所有的段包含,就是把这个因子以及它的所有倍数看成点, 看是 ...
- CONTAINING_RECORD的实现
// 9.25.cpp : 定义控制台应用程序的入口点.///*----------------------------CONTAINING_RECODER的实现------------------- ...
- ibatis入门教程
转载自 http://www.cnblogs.com/ycxyyzw/archive/2012/10/13/2722567.html iBatis 简介: iBatis 是apache 的一个开源项 ...
- 移动混合开发之android文件管理-->flexbox,webFont。
增加操作栏,使用felxbox居中,felx相关参考网址:http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html 使用webFont添加图标, ...
- Maven 常用的命令
运行几个基本的Maven命令 mvn compile 编译主程序 mvn test-compile 编译测试程序 mvn clean 清理 mvn test 测试 mvn pac ...
- OpenCV在VS2005下的配置
http://bbs.ednchina.com/BLOG_ARTICLE_3019518.HTM 这是自己今天刚弄好了之后的一点经历.不是很详细.以后有时间一定要整理整理.
- loadrunner四大部分
loadrunner主要分一下四部分 1.VuGen 主要进行录制,回放,参数化,脚本修改,可以对脚本进行recording options,General options,runtime opti ...