提升程序性能的方法
1、多线程
2、单例模式:整个程序的运行中,只存在一个对象。
饿汉方式:上来不管啥先创建一个对象再说。(不用加锁也是线程安全的)
懒汉方式:当程序启动之后并不会初始化,而是在什么时候调用,什么时候再初始化。
设计模式:
1、单例模式(手写)
2、工厂模式(简单工厂、抽象工厂)
3、模板模式
…
饿汉方式
public class ThreadDemoEH1 {
static class Singleton{
//1、创建私有的构造函数(为了防止其他类直接创建)
private Singleton(){
}
//2、定义私有变量
private static Singleton singleton = new Singleton();
//3、提供公共的获取实例方法
public static Singleton getInstance(){
return singleton;
}
}
public static void main(String[] args) {
Singleton singleton = Singleton.getInstance();
System.out.println(singleton);
}
}
饿汉方式缺点:程序启动之后就会创建,但是创建完了之后有可能不会使用,从而浪费了系统资源。
懒汉方式
1、单线程:
public class ThreadDemoLH1 {
static class Singleton{
//创造一个私有的构造函数,防止其他地方实例化
private Singleton(){
}
//创建一个私有的类对象
private static Singleton singleton = null;
//提供统一的访问入口
public static Singleton getInstance(){
if (singleton==null){
//第一次访问
singleton = new Singleton();
}
return singleton;
}
}
public static void main(String[] args) {
//创建第一个对象
Singleton s1 = Singleton.getInstance();
//创建第二个对象
Singleton s2 = Singleton.getInstance();
System.out.println(s1==s2);
}
}
2、使用一个子线程和主线程一起执行(线程不安全)
public class ThreadDemoLH2 {
static class Singleton{
//创造一个私有的构造函数,防止其他地方实例化
private Singleton(){
}
//创建一个私有的类对象
private static Singleton singleton = null;
//提供统一的访问入口
public static Singleton getInstance() throws InterruptedException {
if (singleton==null){
Thread.sleep(1000);
//第一次访问
singleton = new Singleton();
}
return singleton;
}
}
private static Singleton s1 = null;
private static Singleton s2 = null;
public static void main(String[] args) throws InterruptedException {
//创建新线程任执行任务
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
s1 = Singleton.getInstance();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t1.start();
//使用主线程执行任务
s2 = Singleton.getInstance();
t1.join();
System.out.println(s1==s2);
}
}
可以看到一会是true,一会是false,则证明这种方式是线程不安全的。
3、在使用一个子线程和主线程基础上,对访问入口方法进行加锁
public class ThreadDemoLH3 {
static class Singleton{
//创造一个私有的构造函数,防止其他地方实例化
private Singleton(){
}
//创建一个私有的类对象
private static Singleton singleton = null;
//提供统一的访问入口
public static synchronized Singleton getInstance() throws InterruptedException {
if (singleton==null){
Thread.sleep(1000);
//第一次访问
singleton = new Singleton();
}
return singleton;
}
}
private static Singleton s1 = null;
private static Singleton s2 = null;
public static void main(String[] args) throws InterruptedException {
//创建新线程任执行任务
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
s1 = Singleton.getInstance();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t1.start();
//使用主线程执行任务
s2 = Singleton.getInstance();
t1.join();
System.out.println(s1==s2);
}
}
但是这种性能不佳。
最终版本:使用volatile定义一个私有类对象(保证原子性),并在访问入口方法内部加锁(双重效验)
public class ThreadDemoLH4 {
static class Singleton{
//创造一个私有的构造函数,防止其他地方实例化
private Singleton(){
}
//创建一个私有的类对象(volatile)
private static volatile Singleton singleton = null;
//提供统一的访问入口
public static Singleton getInstance() throws InterruptedException {
if (singleton==null){
synchronized (Singleton.class){
if (singleton==null){
//第一次访问
singleton = new Singleton();
}
}
}
return singleton;
}
}
private static Singleton s1 = null;
private static Singleton s2 = null;
public static void main(String[] args) throws InterruptedException {
//创建新线程任执行任务
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
s1 = Singleton.getInstance();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t1.start();
//使用主线程执行任务
s2 = Singleton.getInstance();
t1.join();
System.out.println(s1==s2);
}
}
实现自定义阻塞队列
自定义阻塞队列:
生产者消费者模型----生产者生产数据(添加数据),消费者消费生产者生产的数据(取出数据)。
当数据满了之后,不要尝试给队列添加数据,而是阻塞等待。sleep(time)不适合 ;使用wait()/notify/notifyAll;LockSupport park()/unpark(thread)
当队列为空,就阻塞等待
自定义阻塞队列:链表、数组
这里用数组实现:
import java.util.Random;
/**
* 自定义阻塞队列
*/
public class ThreadDemoZSDL1 {
static class MyBlockQueue{
private int[] values;//实际存储数据的数组
private int first;//队首
private int last;//队尾
private int size;//队列实际大小
public MyBlockQueue(int initial){
//初始化变量
values = new int[initial];
first = 0;
last = 0;
size = 0;
}
//添加元素(队尾)
public void offer(int val){
synchronized (this) {
//判断边界值
if (size == values.length) {
//队列已满
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//添加元素到队尾
values[last++] = val;
size++;
//判断是否为最后一个元素
if (last == values.length){
last = 0;
}
// 尝试唤醒消费者
this.notify();
}
}
//查询方法
public int poll(){
int result = -1;
synchronized (this) {
//判断边界值
if (size == 0) {
//队列为空,阻塞等待
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
result = values[first++];
size--;
// 判断是否为最后一个元素
if (first==values.length){
first =0;
}
//尝试唤醒生产者
this.notify();
}
return result;
}
}
public static void main(String[] args) {
MyBlockQueue myBlockQueue = new MyBlockQueue(100);
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
//每隔500毫秒生产一条数据
while (true){
int num = new Random().nextInt(10);
System.out.println("生产了随机数:" + num);
myBlockQueue.offer(num);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
t1.start();
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
while(true){
int result = myBlockQueue.poll();
System.out.println("消费了数据:" + result);
}
}
});
t2.start();
}
}