javase 学习

 java背景介绍

 

平台无关性

面向对象,跨平台

java源码----java字节码---jvm----windows,linux。。。。

 

java虚拟机与垃圾回收

虚拟机

内存

    1. 堆 --- 对象
    2. 栈 --- 对象引用、变量
    3. 方法区 --- 定义、常量
              

堆:

年轻代:eden,s0,s1,垃圾回收

老年代:稳定的,一直可用的对象,

 

package com.zst.javsse;

 

public class GcTest {

         public static void main(String[] args) {

                   Object obj = new Object();

                   System.gc();

         }

}

 

右键,run as config---vm

--打印GC信息

-XX:+PrintGCDetails

-Xms5m -Xmx20m -XX:+PrintGCDetails -XX:+UseSerialGC -XX:+PrintCommandLineFlags

 

 

[GC [PSYoungGen: 1331K->584K(38400K)] 1331K->584K(124928K), 0.0016352 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]

[Full GC [PSYoungGen: 584K->0K(38400K)] [ParOldGen: 0K->463K(86528K)] 584K->463K(124928K) [PSPermGen: 2573K->2572K(21504K)], 0.0145588 secs] [Times: user=0.05 sys=0.00, real=0.01 secs]

Heap

 PSYoungGen      total 38400K, used 998K [0x00000007d5b00000, 0x00000007d8580000, 0x0000000800000000)

  eden space 33280K, 3% used [0x00000007d5b00000,0x00000007d5bf9a60,0x00000007d7b80000)

  from space 5120K, 0% used [0x00000007d7b80000,0x00000007d7b80000,0x00000007d8080000)

  to   space 5120K, 0% used [0x00000007d8080000,0x00000007d8080000,0x00000007d8580000)

 ParOldGen       total 86528K, used 463K [0x0000000781200000, 0x0000000786680000, 0x00000007d5b00000)

  object space 86528K, 0% used [0x0000000781200000,0x0000000781273dd8,0x0000000786680000)

 PSPermGen       total 21504K, used 2579K [0x000000077c000000, 0x000000077d500000, 0x0000000781200000)

  object space 21504K, 11% used [0x000000077c000000,0x000000077c284ee0,0x000000077d500000)

 

package com.zst.javsse;

 

public class GcTest2 {

  public static void main(String[] args) {

     System.out.println("start");

     System.out.println("max memory:"+Runtime.getRuntime().maxMemory());

     System.out.println("free memory:"+Runtime.getRuntime().freeMemory());

     System.out.println("total memory:"+Runtime.getRuntime().totalMemory());

    

     System.out.println("1m");

     byte[] b1 = new byte[1024*1024];

     System.out.println("max memory:"+Runtime.getRuntime().maxMemory());

     System.out.println("free memory:"+Runtime.getRuntime().freeMemory());

     System.out.println("total memory:"+Runtime.getRuntime().totalMemory());

    

     System.out.println("4m");

     byte[] b2 = new byte[4*1024*1024];

     System.out.println("max memory:"+Runtime.getRuntime().maxMemory());

     System.out.println("free memory:"+Runtime.getRuntime().freeMemory());

     System.out.println("total memory:"+Runtime.getRuntime().totalMemory());

    

  }

}

 

 

垃圾回收

强制垃圾回收:System.gc,可能不会马上执行,需要等待调度

 

java面向对象基础知识

语言特点

面向对象

健壮性---垃圾回收,没有指针,使用引用

安全性

分布性---jvm

多线程

面向对象特征:

封装---安全

继承---代码复用

多态---面向接口编程,工厂模式

 

多使用组合,少使用继承

 

面向过程:自顶向下,函数模块,算法+数据结构

面向对象:自底向上,对象

 

对象:数据和函数的封装

类:相同属性和行为的集合

 

封装:属性私有,方法公开

static:

静态成员变量:只有一份内存拷贝,作用域类内部,类名访问,需要初始化

静态成员方法

包:文件夹,解决重名

 

package:打包

import:导包

缺省包

 

对象数组:

 

继承:只支持单继承

父类的构造方法不被子类继承,子类需要使用super()调用

 

多态

动态多态

运行时才能确认调用方法

必须有继承

有方法覆盖

有父类引用指向子类对象

静态多态

编译时多态,编译时决定使用哪个方法

方法重载

跟继承没必然关系

 

抽象类

包含抽象方法,

不能实例

需要继承

接口

全是抽象方法

需要实现

 

final

修饰变量----常量

修饰方法---不能覆盖

修饰类---不能继承

单例:

异常

编译异常

运行异常

多线程

Thread类

Runnable接口

多任务:

 

demo1

package com.zst.ThreadDemo;

 

import java.util.concurrent.CountDownLatch;

 

public class ThreadTest {

        

         private static boolean isok = false;

         static final CountDownLatch countdown = new CountDownLatch(1);

        

        

         public static void main(String[] args) {

                  

                   final Object lock = new Object();

                  

                   Thread washThread = new Thread(new Runnable() {

                            @Override

                            public void run() {

                                     for(int i=1;i<=5;i++) {

                                              

                                               System.out.println("washing:"+i);

                                               try {

                                                        Thread.sleep(1500);

                                               } catch (InterruptedException e) {

                                                        e.printStackTrace();

                                               }

                                               if(i==3) {

                                                        System.out.println("发出通知");

                                                        countdown.countDown();

                                               }

                                     }

                            }

                           

                   },"washthread");

                  

                   Thread boilThread = new Thread(new Runnable() {

 

                            @Override

                            public void run() {

                                    

                                               try {

                                                        countdown.await();

                                               } catch (InterruptedException e) {

                                                        e.printStackTrace();

                                               }

                                               System.out.println("收到通知...make tea...");

                            }

                           

                   },"boilthread");

washThread.start();

boilThread.start();

 

 

demo2

package com.zst.ThreadDemo;

 

import java.util.concurrent.CountDownLatch;

 

public class ThreadTest {

        

         private static boolean isok = false;

         static final CountDownLatch countdown = new CountDownLatch(2);

        

        

         public static void main(String[] args) {

                  

                   final Object lock = new Object();

                  

                   Thread washThread = new Thread(new Runnable() {

                            @Override

                            public void run() {

                                     for(int i=1;i<=5;i++) {

                                              

                                               System.out.println("washing:"+i);

                                               try {

                                                        Thread.sleep(1500);

                                               } catch (InterruptedException e) {

                                                        e.printStackTrace();

                                               }

                                     }

                                     System.out.println("发出通知");

                                     countdown.countDown();

                            }

                           

                   },"washthread");

                  

                   Thread boilThread = new Thread(new Runnable() {

 

                            @Override

                            public void run() {

                                    

                                               try {

                                                        Thread.sleep(5000);

                                               } catch (InterruptedException e) {

                                                        e.printStackTrace();

                                               }

                                               System.out.println("收到通知...make tea...");

                                               countdown.countDown();

                            }

                           

                   },"boilthread");

                   Thread maketeaThread = new Thread(new Runnable() {

 

                            @Override

                            public void run() {

                                    

                                               try {

                                                        countdown.await();

                                               } catch (InterruptedException e) {

                                                        e.printStackTrace();

                                               }

                                               System.out.println("收到通知... 喝茶...");

                            }

                           

                   },"maketeathread");

                   washThread.start();

                   boilThread.start();

                   maketeaThread.start();

         }

         }

 

线程优先级,默认为5,1-10,以我水平来说没啥用

生命周期:新建---就绪(start)—运行(run)—死亡,等待(notify,interupt)---阻塞,阻塞(同步锁)---就绪,睡眠挂起(sleep,join,等待io)---就绪

 

后台线程:setDaemon(),启动前设置为true,为其他线程提供服务的线程,也叫守护线程或者精灵线程,比如说垃圾回收线程,只有前台线程结束生命周期,后台线程才会结束

 

生产者与消费者模式:生产者—缓冲区---消费者

线程池

线程池(缓冲区):先启动一堆线程,处于睡眠状态,当有请求的时候,唤醒其中一个线程,处理请求,处理完了,有进入睡眠状态

解决生命周期开销问题和资源不足

 

Synchronized

package com.zst.ThreadDemo;

 

public class Synchronized implements Runnable{

 

 

        

         @Override

         public synchronized void run() {

                   for(int i=0;i<5;i++) {

                            System.out.println(Thread.currentThread().getName()+"---"+i);

                            try {

                                     Thread.sleep(1000);

                            } catch (InterruptedException e) {

                                     // TODO Auto-generated catch block

                                     e.printStackTrace();

                            }

                           

                   }

         }

 

         public static void main(String[] args) {

                   Synchronized t1 = new Synchronized();

                   Thread ta = new Thread(t1);

                   Thread tb = new Thread(t1);

                   Thread tc = new Thread(t1);

                   ta.start();

                   tb.start();

                   tc.start();

         }

 

}

 

fixThreadPoolTest

package com.zst.ThreadDemo;

 

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

 

public class FixThreadPoolTest {

         public static void main(String[] args) {

                   ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);

                   for (int i = 0;i<10;i++) {

                            final int index = i;

                  

                            fixedThreadPool.execute(new Runnable() {

 

                                     @Override

                                     public void run() {

                                               // TODO Auto-generated method stub

                                               try {

                                                        Thread.sleep(1000);

                                               } catch (InterruptedException e) {

                                                        // TODO Auto-generated catch block

                                                        e.printStackTrace();

                                               }

                                               System.out.println(Thread.currentThread().getName()+"---"+index);

                                     }

                                    

                            });

                           

                   }

         }

}

 

Cached

package com.zst.ThreadDemo;

 

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

 

public class CachedThreadPoolTest {

         public static void main(String[] args) {

                   ExecutorService cachedThreadPool = Executors.newCachedThreadPool();

                   for (int i = 0;i<10;i++) {

                            final int index = i;

                           

                            cachedThreadPool.execute(new Runnable() {

 

                                     @Override

                                     public void run() {

                                               // TODO Auto-generated method stub

                                               try {

                                                        Thread.sleep(1000);

                                               } catch (InterruptedException e) {

                                                        // TODO Auto-generated catch block

                                                        e.printStackTrace();

                                               }

                                               System.out.println(Thread.currentThread().getName()+"---"+index);

                                     }

                                    

                            });

                           

                   }

         }

}

 

scheduled

package com.zst.ThreadDemo;

 

import java.util.Date;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.ScheduledThreadPoolExecutor;

import java.util.concurrent.TimeUnit;

 

public class ScheduleThreadPoolTest {

         public static void  main(String[] args) {

                   ScheduledThreadPoolExecutor scheduleThreadPool = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(5);

                   scheduleThreadPool.scheduleAtFixedRate(new Runnable() {

 

                            @Override

                            public void run() {

                                     // TODO Auto-generated method stub

                                     System.out.println(Thread.currentThread().getName()+"---"+new Date());

                            }

                           

                   }, 5, 1, TimeUnit.SECONDS);

 

                  

         }

}

 

single

 

package com.zst.ThreadDemo;

 

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

 

public class SingleThreadPoolTest {

         public static void main(String[] args) {

                   ExecutorService singleThreadPool = Executors.newSingleThreadExecutor();

                   for (int i = 0;i<10;i++) {

                            final int index = i;

                           

                            singleThreadPool.execute(new Runnable() {

 

                                     @Override

                                     public void run() {

                                               // TODO Auto-generated method stub

                                               try {

                                                        Thread.sleep(1000);

                                               } catch (InterruptedException e) {

                                                        // TODO Auto-generated catch block

                                                        e.printStackTrace();

                                               }

                                               System.out.println(Thread.currentThread().getName()+"---"+index);

                                     }

                                    

                            });

                           

                   }

         }

}

 

ThreadPoolTest

package com.zst.ThreadDemo;

 

import java.util.concurrent.ArrayBlockingQueue;

import java.util.concurrent.ThreadPoolExecutor;

import java.util.concurrent.TimeUnit;

 

public class ThreadPoolExecutorTest {

         public static void main(String[] args) {

                   ThreadPoolExecutor pool = new ThreadPoolExecutor(1, 1, 60,

                                     TimeUnit.SECONDS,

                                     new ArrayBlockingQueue(5));

                   Task t1 = new Task(1,"Task1");

                   Task t2 = new Task(2,"Task2");

                   Task t3 = new Task(3,"Task3");

                   Task t4 = new Task(4,"Task4");

                   Task t5 = new Task(5,"Task5");

                   Task t6 = new Task(6,"Task6");

                  

                   pool.execute(t1);

                   pool.execute(t2);

                   pool.execute(t3);

                   pool.execute(t4);

                   pool.execute(t5);

                   pool.execute(t6);

         }

}

集合

list

set

map

arrary

vector:线程安全,上面的线程不安全

将多个元素组成一个单元的对象

长度没有限制,类型可以不同

提供一系列的方法

 

 

文件io

File类

 

stream(流):
input

output

字节流

字符流

 

fileInputstream类  ---- 读,字节方式,效率较高

fileoutputstream类 --- 写,字节方式,效率较高

 

字符流,在字节的基础上进行一层包装

FileReader,FileWrite

BufferedReader,BufferedWriter

 

DataInputstream,DataOutputStream

可以输入任何类型的文件,需要其他的流配合

 

对象序列化:

实现serializable接口的对象,可以转换成字节序列

使用OutputStream类

 

Properties类

类似键值对的读取

 

 

数据库

 

反射

运行状态获取这个类的属性和方法。

 

JDBC

需要添加mysql的jar包

 

你可能感兴趣的:(javase 学习)