java性能调优03

Posted YL10000

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java性能调优03相关的知识,希望对你有一定的参考价值。

1、java中的四种引用类型(级别由高到低为:强引用,软引用,弱引用和虚引用)

  1.1 强引用:默认创建的变量都是强引用,垃圾回收机制不会将其回收,当内存空 间不足,Java虚拟机宁愿抛出OutOfMemoryError错误,使程序异常终止

  1.2 软引用(SoftReference):内存不足时,垃圾回收器就会回收它

  1.3 弱引用(WeakReference):只要进行垃圾回收,就会把其回收

  1.4 虚引用(PhantomReference):在任何时候都 可能被回收

//引用队列
ReferenceQueue<ReferenceObject> referenceQueue = new ReferenceQueue<ReferenceObject>();
//强引用
ReferenceObject referenceObject=new ReferenceObject("强引用");
//软引用
SoftReference<ReferenceObject> softReference=new SoftReference<ReferenceObject>(new ReferenceObject("软引用"));
//弱引用
WeakReference<ReferenceObject> weakReference=new WeakReference<ReferenceObject>(new ReferenceObject("弱引用"),referenceQueue);
//虚引用
PhantomReference<ReferenceObject> phantomReference=new PhantomReference<ReferenceObject>(new ReferenceObject("虚引用"), referenceQueue);
System.out.println("强引用:"+referenceObject);//强引用:ReferenceObject [name=强引用]
System.out.println("软引用:"+softReference.get());//软引用:ReferenceObject [name=软引用]
System.out.println("弱引用:"+weakReference.get());//弱引用:ReferenceObject [name=弱引用]
System.out.println("虚引用:"+phantomReference.get());//虚引用:null

System.gc();//虚引用被回收了  弱引用被回收了
System.out.println("========gc==========");
System.out.println("强引用:"+referenceObject);//强引用:ReferenceObject [name=强引用]
System.out.println("软引用:"+softReference.get());//软引用:ReferenceObject [name=软引用]
System.out.println("弱引用:"+weakReference.get());//弱引用:null
System.out.println("虚引用:"+phantomReference.get());//虚引用:null

System.out.println("========内存不知时gc==========");
byte[] bigByte=new byte[2015*1250*590];//软引用被回收了

2、future模式

  核心思想:通过开启子线程来代替主线程处理比较耗时的操作,子线程处理完后通知主线程来获取数据

/**
 * Data
 * @Description 请求接口
 */
public interface Data {
    String getResult();
}

/**
 * RealData
 * @Description 真实数据比较耗时
 */
public class RealData implements Data {
    
    private final String result;
    
    public RealData(String send) throws InterruptedException {
        StringBuffer buffer=new StringBuffer();
        for(int i=0;i<10;i++){
            buffer.append(send);
            Thread.sleep(100);//模拟真实数据比较耗时
        }
        result=buffer.toString();
    }

    @Override
    public String getResult() {
        return result;
    }
}
/** * FutureData * @Description 功能详细描述 */ public class FutureData implements Data { private RealData realData; private boolean isReady=false; public synchronized void setRealData(RealData realData) { if (isReady) { return; } this.realData = realData; isReady=true; notifyAll(); } @Override public synchronized String getResult(){ while (!isReady) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } return realData.getResult(); } } /** * Client * @Description 客户端请求 */ public class Client { public Data request(final String seed) { final FutureData data=new FutureData(); new Thread(){ @Override public void run() { try { RealData realData=new RealData(seed); data.setRealData(realData); } catch (InterruptedException e) { e.printStackTrace(); } } }.start(); return data; } } /** * Test * @Description future模式测试类 */ public class Test { public static void main(String[] args) throws InterruptedException { Client client=new Client(); Data data=client.request("北风网"); System.out.println("请求结束"); Thread.sleep(2000);//其他工作 System.out.println(data.getResult()); } }

 3、master-worker模式

  核心思想:master接受到任务后分给多个worker去执行,worker执行完后把结果返还给master,再由master将结果合并后返给请求者

/**
 * Worker
 */
public class Worker implements Runnable {
    //任务列表
    Queue<Object> taskQueue;
    //结果集
    Map<String, Object> resultMap;

    public void setTaskQueue(Queue<Object> taskQueue) {
        this.taskQueue = taskQueue;
    }

    public void setResultMap(Map<String, Object> resultMap) {
        this.resultMap = resultMap;
    }
    
    //worker的具体业务逻辑处理
    public Object Handle(Object input){
        return input;
    }

    @Override
    public void run() {
        while (true) {
            //获取子任务
            Object input=taskQueue.poll();
            if (input==null)break;
            Object re=Handle(input);
            resultMap.put(input.hashCode()+"", re);
        }
    }
}

/**
 * Master
 */
public class Master {
    //任务队列
    private Queue<Object> taskQueue=new ConcurrentLinkedQueue<Object>();
    //worker线程队列
    private Map<String, Thread> threadMap=new HashMap<String, Thread>();
    //子任务结果集
    private Map<String, Object> resultMap=new HashMap<String, Object>();
    
    //判断所有的子任务是否已经都完成
    public boolean isComplete() {
        for(Entry<String, Thread> entry:threadMap.entrySet()){
            if (entry.getValue().getState()!= Thread.State.TERMINATED) {
                return false;
            }
        }
        return true;
    }
    
    public Master(Worker worker,Integer workerCount){
        worker.setResultMap(resultMap);
        worker.setTaskQueue(taskQueue);
        for(int i=0;i<workerCount;i++){
            threadMap.put(i+"", new Thread(worker,i+""));
        }
    }
    
    public void submit(Object job) {
        taskQueue.add(job);
    }
    
    public Map<String, Object> getResultMap() {
        return resultMap;
    }
    
    public void execute() {
        for(Entry<String, Thread> entry:threadMap.entrySet()){
            entry.getValue().start();
        }
    }
}

public class PlusWorker extends Worker {
    @Override
    public Object Handle(Object input) {
        int i=(int) input;
        return i*i*i;
    }
}

/**
 * Test
 * @Description 测试master-worker模式
 */
public class Test {
    public static void main(String[] args) {
        Master master=new Master(new PlusWorker(), 5);
        for(int i=0;i<100;i++){
            master.submit(i);
        }
        master.execute();
        Map<String, Object> resultMap=master.getResultMap();
        int sum=0;
        while (resultMap.size()>0|| !master.isComplete()) {
            String key=null;
            for(String k:resultMap.keySet()){
                key=k;
                break;
            }
            if (key!=null&&resultMap.get(key)!=null) {
                sum+=(Integer)resultMap.remove(key);
            }
        }
        System.out.println(sum);
    }
}

 4、guardedSuspension模式(保护暂停模式)

  核心思想:当客户端大量请求来请求服务器时,将请求放入到请求队列中,服务器按照队列顺序依次来处理客户端的请求

/**
 * Request
 * @Description 请求
 */
public class Request {
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Request(String name) {
        super();
        this.name = name;
    }
    @Override
    public String toString() {
        return "Request [name=" + name + "]";
    }
}

/**
 * RequestQueue
 * @Description 请求队列
 */
public class RequestQueue {
    //请求队列
    private LinkedList<Request> queue=new LinkedList<Request>();
    
    //服务器获取请求
    public synchronized Request getRequest() throws InterruptedException {
        while (queue.size()==0) {
            this.wait();
        }
        return queue.remove();
    }
    
    //客户端添加请求
    public synchronized void addRequest(Request request) {
        queue.add(request);
        this.notifyAll();
    }
}

/**
 * ClientThread
 * @Description 客户端请求
 */
public class ClientThread extends Thread {
    private RequestQueue requestQueue;
    
    public ClientThread(RequestQueue requestQueue,String threadName){
        super(threadName);
        this.requestQueue=requestQueue;
    }

    @Override
    public void run() {
        for(int i=0;i<10;i++){
            Request request=new Request("当前线程"+Thread.currentThread().getName()+"请求"+i);
            requestQueue.addRequest(request);
            try {
                sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("当前线程"+Thread.currentThread().getName()+"请求结束");
    } 
}

/**
 * ServerThread
 * @Description 服务器处理请求
 */
public class ServerThread extends Thread{
    //请求队列
    private RequestQueue queue;
    
    public ServerThread(RequestQueue queue,String threadName){
        super(threadName);
        this.queue=queue;
    }

    @Override
    public void run() {
        while (true) {
            Request request;
            try {
                request = queue.getRequest();
                System.out.println("当前线程"+Thread.currentThread().getName()+"处理请求"+request.toString());
                sleep(100);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

/**
 * 
 * Test
 * @Description 保护暂停模式测试
 */
public class Test {
    public static void main(String[] args) {
        RequestQueue queue=new RequestQueue();
        //服务器开启10个线程处理客户端的请求
        for(int i=0;i<10;i++){
            new ServerThread(queue, "server"+i).start();
        }
        
        //模拟10个客户端向服务器发起请求
        for(int i=0;i<10;i++){
            new ClientThread(queue, "client"+i).start();
        }
    }
}

 5、生产者消费者模式

  核心思想:生产者把生产好的数据放入到中间缓冲区(队列中),消费者冲从中间缓冲中获取数据进行消费

  

/**
 * PcData
 * @Description 数据
 */
public class PcData {
    private final Integer data;

    public PcData(Integer data) {
        this.data = data;
    }

    public Integer getData() {
        return data;
    }

    @Override
    public String toString() {
        return "PcData [data=" + data + "]";
    }
}

/**
 * Productor
 * @Description 生产者
 */
public class Productor implements Runnable{
    /**
     * volatile 声明的变量表示是不稳定的,每次使用它时必须从主存(共享内存)中进行读取,
     *  每次修改后,强迫线程将变化后的值写回到共享内存中
     *  这样保证任何时候,两个不同线程总是看到某个变量的同一个值
     */
    private volatile boolean isRunning=true;
    
    //数据队列
    private BlockingQueue<PcData> blockingQueue=null;
    
    private static AtomicInteger count=new AtomicInteger();
    
    public Productor(BlockingQueue<PcData> blockingQueue){
        this.blockingQueue=blockingQueue;
    }
    
    public void stop(){
        this.isRunning=false;
    }

    @Override
    public void run() {
        PcData data=null;
        Random random=new Random();
        System.out.println("启动生产者:"+Thread.currentThread().getId());
        while (isRunning) {
            try {
                Thread.sleep(random.nextInt(1000));
                data=new PcData(count.incrementAndGet());
                System.out.println(data+"放入到了队列中");
                //如果空间不足等待2秒
                if (blockingQueue.offer(data, 2, TimeUnit.SECONDS)) {
                    System.out.println(data+"已经放入到了队列中");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            
        }
    }
}

/**
 * Customer
 * @Description 消费者
 */
public class Customer implements Runnable {
    
    private BlockingQueue<PcData> blockingQueue;
    
    public Customer(BlockingQueue<PcData> blockingQueue){
        this.blockingQueue=blockingQueue;
    }

    @Override
    public void run() {
        System.out.println("启动消费者:"+Thread.currentThread().getId());
        Random random=new Random();
        while (true) {
            PcData data;
            try {
                data = this.blockingQueue.take();
                if (data!=null) {
                    System.out.println("消费了数据:"+data.toString());
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }   
        }
    }
}

/**
 * Test
 * @Description 测试生产者消费者模式
 */
public class Test {
    public static void main(String[] args) {
        //数据列表
        BlockingQueue<PcData> blockingQueue=new LinkedBlockingQueue<PcData>(10);
        //生产者
        Productor productor01=new Productor(blockingQueue);
        Productor productor02=new Productor(blockingQueue);
        Productor productor03=new Productor(blockingQueue);
        //消费者
        Customer customer01=new Customer(blockingQueue);
        Customer customer02=new Customer(blockingQueue);
        Customer customer03=new Customer(blockingQueue);
        //使用线程池节省开销
        ExecutorService executorService=Executors.newCachedThreadPool();
        executorService.execute(productor01);
        executorService.execute(productor02);
        executorService.execute(productor03);
        executorService.execute(customer01);
        executorService.execute(customer01);
        executorService.execute(customer01);
    }
}

 

  






























以上是关于java性能调优03的主要内容,如果未能解决你的问题,请参考以下文章

面试必备:深入 Java 应用性能调优实践

5年的 Java 代码性能这么低下,如何调优?

Java 应用性能调优实践

北大青鸟java培训:Java性能调优都有哪些技巧?

如何轻松搞定 Java 性能调优?层层调优的实践总结

Java 应用性能调优最强实践指南!