如何保证类在内存中只有一个对象呢?java
public class Demo5_Singleton { //单列设计模式,保证类在内存中只有一个对象 public static void main(String[] args) { Singleton s1 = Singleton.s; //成员变量私有,不能经过类名.调用,s和s1指向的是同一个对象 // Singleton.s = null; Singleton s2 = Singleton.s; System.out.println(s1 == s2); //比较引用地址值 /*Singleton s1 = Singleton.getInstance(); Singleton s2 = Singleton.getInstance(); System.out.println(s1 == s2);*/ } } //(1)饿汉式-简单直接-空间换时间-开发中使用 /*class Singleton { //1.私有构造方法,其余类不能访问该构造方法 private Singleton() {} //2.建立本类对象 public static Singleton s = new Singleton(); //3.对外提供公共的访问方法 public static Singleton getInstance() { //获取实例(对象) return s; } }*/ //(2)懒汉式-单例延迟加载模式-时间换空间-开发不用 /*class Singleton { //1.私有构造方法,其余类不能访问该构造方法 private Singleton() {} //2.声明一个引用 public static Singleton s ; //3.对外提供公共的访问方法 public static Singleton getInstance() { //获取实例(对象) if(s == null) { //线程1等待,线程2等待,有可能建立多个对象 s = new Singleton(); } return s; } }*/ //(3)第三种格式-使用final修饰 class Singleton { //1.私有构造方法,其余类不能访问该构造方法 private Singleton() {} //2.声明一个引用 public final static Singleton s = new Singleton(); }
import java.io.IOException; public class Demo6_Runtime { public static void main(String[] args) throws IOException { Runtime r = Runtime.getRuntime(); //获取运行时对象 // r.exec("shutdown -s -t 3000"); r.exec("shutdown -a"); } }
import java.util.Date; import java.util.Timer; import java.util.TimerTask; public class Demo7_Timer { public static void main(String[] args) throws InterruptedException { Timer t = new Timer(); //在指定时间安排指定任务:安排任务.执行时间.重复执行时间 //年=当前-1900 月=当前-1 t.schedule(new MytimerTask(), new Date(118, 9, 11, 11, 57, 30),3000); while(true) { Thread.sleep(1000); System.out.println(new Date()); } } } class MytimerTask extends TimerTask { @Override public void run() { System.out.println("起床上班"); } }
1.何时须要通讯程序员
2.怎么通讯设计模式
public class Demo8_Notify { //等待唤醒机制 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(); } } }
1.多个线程通讯的问题多线程
3.为何wait方法和notify方法定义在Object这类中?并发
4.sleep方法和wait方法的区别?ide
public class Demo9_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(); } } class Printer2 { private int flag = 1; public void print1() throws InterruptedException { synchronized(this) { while(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.notifyAll(); //随机唤醒单个等待线程 } } public void print2() throws InterruptedException { synchronized(this) { while(flag != 2) { this.wait(); //线程2等待 } System.out.print("学"); System.out.print("习"); System.out.print("编"); System.out.print("程"); System.out.print("\r\n"); flag = 3; this.notifyAll(); } } public void print3() throws InterruptedException { synchronized(this) { while(flag != 3) { this.wait(); //if语句在哪里等待,在哪里唤醒,while是循环判断 } System.out.print("天"); System.out.print("天"); System.out.print("向"); System.out.print("上"); System.out.print("\r\n"); flag = 1; this.notifyAll(); } } }
1.同步函数
2.通讯布局
import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; public class Demo91_ReentrantLock { 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("1"); System.out.print("1"); System.out.print("1"); System.out.print("1"); System.out.print("\r\n"); flag = 2; c2.signal(); r.unlock(); //释放锁 } public void print2() throws InterruptedException { r.lock(); if(flag != 2) { c2.await(); } System.out.print("2"); System.out.print("2"); System.out.print("2"); System.out.print("2"); System.out.print("\r\n"); flag = 3; c3.signal(); r.unlock(); } public void print3() throws InterruptedException { r.lock(); if(flag != 3) { c3.await(); } System.out.print("3"); System.out.print("3"); System.out.print("3"); System.out.print("3"); System.out.print("\r\n"); flag = 1; c1.signal(); r.unlock(); } }
A:线程组概述性能
默认状况下,全部的线程都属于主线程组。测试
咱们也能够给线程设置分组
B:案例演示
public class Demo4_ThreadGroup { public static void main(String[] args) { // demo1(); //本身设定线程组 ThreadGroup tg = new ThreadGroup("我是一个新的线程组"); //建立新的线程组 MyRunnable2 mr = new MyRunnable2(); //建立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() { //默认是主线程组 MyRunnable2 mr = new MyRunnable2(); 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 MyRunnable2 implements Runnable { @Override public void run() { for(int i = 0; i < 1000; i++) { System.out.println(Thread.currentThread().getName() + "...." + i); } } }
A:线程池概述
B:内置线程池的使用概述
JDK5新增了一个Executors工厂类来产生线程池,有以下几个方法
使用步骤:
C:案例演示
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Demo93_Executors { public static void main(String[] args) { ExecutorService pool = Executors.newFixedThreadPool(2); //建立线程池 // 能够执行Runnable对象或者Callable对象表明的线程 pool.submit(new MyRunnable2()); //将线程放进池子里并执行 pool.submit(new MyRunnable2()); pool.shutdown(); //关闭线程池 } }
多线程程序实现的方式3的好处
弊端:
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 Demo94_Callable { public static void main(String[] args) throws InterruptedException, ExecutionException { ExecutorService pool = Executors.newFixedThreadPool(2); //建立线程池 Future<Integer> f1 = pool.submit(new MyCallable(100)); //将线程放进池子里并执行 Future<Integer> f2 = pool.submit(new MyCallable(50)); System.out.println(f1.get()); System.out.println(f2.get()); pool.shutdown(); //关闭线程池 } } class MyCallable implements Callable<Integer> { private int num; public MyCallable(int num) { this.num = num; } @Override public Integer call() throws Exception { int sum = 0; for(int i = 1; i <= num; i++) { sum += i; } return sum; } }
A:简单工厂模式概述
B:优势
C:缺点
D:案例演示
public abstract class Animal { public abstract void eat(); }
public class Dog extends Animal { @Override public void eat() { System.out.println("狗吃肉"); } }
public class Cat extends Animal { @Override public void eat() { System.out.println("猫吃鱼"); } }
public class AnimalFactory { /*public static Dog createDog() { return new Dog(); } public static Cat CreateCat() { return new Cat(); }*/ //发现方法会定义不少,复用性差 public static Animal createAnimal(String name) { if("dog".equals(name)) { return new Dog(); }else if("cat".equals(name)) { return new Cat(); }else { return null; } } }
public class Test { public static void main(String[] args) { // Dog d = AnimalFactory.createDog(); Dog d = (Dog) AnimalFactory.createAnimal("dog"); d.eat(); Cat c = (Cat) AnimalFactory.createAnimal("cat"); c.eat(); } }
A:工厂方法模式概述
B:优势
C:缺点
动物抽象类:public abstract Animal { public abstract void eat(); } 工厂接口:public interface Factory { public abstract Animal createAnimal(); } 具体狗类:public class Dog extends Animal { public void eat() { System.out.println("狗吃肉"); } } 具体猫类:public class Cat extends Animal { public void eat() { System.out.println("猫吃鱼"); } } 开始,在测试类中每一个具体的内容本身建立对象,可是,建立对象的工做若是比较麻烦,就须要有人专门作这个事情, 因此就知道了一个专门的类来建立对象。发现每次修改代码太麻烦,用工厂方法改进,针对每个具体的实现提供一个具体工厂。 狗工厂:public class DogFactory implements Factory { public Animal createAnimal() { return new Dog(); } } 猫工厂:public class CatFactory implements Factory { public Animal createAnimal() { return new Dog(); } } 测试类: public class Test { public static void main(String[] args) { DogFactory df = new DogFactory(); Dog d = (Dog) df.createAnimal(); d.eat(); } }
Frame f = new Frame(“my window”); f.setLayout(new FlowLayout());//设置布局管理器 f.setSize(500,400);//设置窗体大小 f.setLocation(300,200);//设置窗体出如今屏幕的位置 f.setIconImage(Toolkit.getDefaultToolkit().createImage("qq.png")); f.setVisible(true);
FlowLayout(流式布局管理器)
BorderLayout(边界布局管理器)
GridLayout(网格布局管理器)
CardLayout(卡片布局管理器)
GridBagLayout(网格包布局管理器)
Frame f = new Frame("个人窗体"); //事件源是窗体,把监听器注册到事件源上 //事件对象传递给监听器 f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { //退出虚拟机,关闭窗口 System.exit(0); } });
a.什么是适配器
b.适配器原理
事件处理
public class Demo1_Adapter { /* * @param args * 适配器设计模式 * 鲁智深 * */ public static void main(String[] args) { } } interface 和尚 { public void 打坐(); public void 念经(); public void 撞钟(); public void 习武(); } abstract class 天罡星 implements 和尚{ //声明成抽象的缘由是,不想让其余类建立本类对象,由于建立也没有意义 @Override public void 打坐() { } @Override public void 念经() { } @Override public void 撞钟() { } @Override public void 习武() { } } class 鲁智深 extends 天罡星{ public void 习武() { System.out.println("倒拔垂杨柳"); System.out.println("拳打镇关西"); System.out.println("大闹野猪林"); System.out.println(".........."); } }