java基础笔记备忘---不定时更新

函数、方法

java是值传递

方法重载

参数类型、个数、参数顺序不同

可变参数

  • 类型后面是…
  • 只能是最后一个参数

递归

  • 递归条件

    方便程序员,不方便电脑

数组

声明数组
int[] nums;    //建议java使用这个
int nums2[];
定义数组
int[] nums = new int[10];
获取数组长度
nums.length;
三种初始化
  • 静态初始化
    int[] a = {1,2,3};
    Man mans = {new man(),new man()};
    
  • 动态初始化
    int[] b = new int[2];
    b[0]=0;
    b[1]=1;
    
  • 默认初始化

    没有赋值的元素默认是0

数组特点
  • 长度确定不可改
  • 必须同类型
  • 数据可以是数据类型也可以是引用类型
  • 数组变量属于引用类型
for each遍历数组
int[] arrays = {1,2,3,4,5};
for (int array:arrays){
    System.out.println(array);
}
Arrays类

接口

  • 接口方法默认public abstract
  • 接口的属性默认public static final

多线程

如何创建

方法1
  1. 自定义线程类继承Thread方法
  2. 重写run()方法
  3. 创建线程对象,调用strat()方法启动线程
public class TestThread extends Thread {
    public static void main(String[] args) {
        TestThread testThread = new TestThread();
        testThread.start();
        for (int i = 0; i < 20; i++) {
            System.out.println("我在看代码");
        }
    }
    @Override
    public void run() {
        System.out.println("我在学习多线程");
    }
}
方法2
  1. 自定义类XXX实现Runnable接口
  2. 实现run()方法
  3. 实例化一个XXX对象
  4. new 一个Thread对象传入参数xxx,调用start()方法
public class TestRunnable implements Runnable {
    public static void main(String[] args) {
        TestRunnable testRunnable = new TestRunnable();
        new Thread(testRunnable,"这里是线程名字").start();
    }
    @Override
    public void run() {
        System.out.println("多线程");
    }
}

线程状态

  • 创建
  • 就绪
  • 运行
  • 阻塞
  • 死亡

如何停止线程

  • 不要用stop,
  • 建议使用标志位flag停止线程
package 线程;

public class TestRunnable implements Runnable {
    // 1.设置一个标志位
    private boolean flag = true;

    @Override
    public void run() {
        while (flag) {
            System.out.println("多线程");
        }
    }
    // 2.设置一个公开的方法停止线程,转换标志位
    public void stop(){
        this.flag = false;
    }

    public static void main(String[] args) {
        TestRunnable testRunnable = new TestRunnable();
        new Thread(testRunnable).start();
        for (int i = 0; i < 5; i++) {
            System.out.println("Hi");
            if (i == 3)
                testRunnable.stop();
        }
    }
}

线程休眠

  • sleep(时间),时间是毫秒数
  • 模拟延迟为了发现错误

线程礼让

  • 将线程从运行转为就绪
  • 礼让不一定成功,看CPU心情
  • Thread.yield()

强制执行

join();

会让其他线程全部阻塞

package 线程;

public class TestJoin implements Runnable{

    @Override
    public void run() {
        for (int i = 0; i < 200; i++) {
            System.out.println("我是vip");
        }

    }

    public static void main(String[] args) throws InterruptedException {
        TestJoin testJoin = new TestJoin();
        Thread thread = new Thread(testJoin);
        thread.start();
        for (int i = 0; i < 200; i++) {
            if (i==50)
                thread.join();
            System.out.println("我是主线程"+i);
        }
    }
}

观测状态

getState();

public class TestState {
    public static void main(String[] args) throws InterruptedException {


        Thread thread = new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("///");
        });
        // 观察状态
        Thread.State state = thread.getState();
        System.out.println(state);
        // 运行状态
        thread.start();//启动线程
        state = thread.getState();
        while (state != Thread.State.TERMINATED){
            Thread.sleep(100);
            state = thread.getState();
            System.out.println(state);

        }
    }
}

线程优先级

setPriority();

优先级只代表获得CPU调度的概率,不一定就是优先级高的先执行

守护线程(daemon)

  • 线程分为用户线程和守护线程
  • 虚拟机必须确保用户线程执行完毕
  • 虚拟机不用等待守护线程执行完毕
  • 如:后台操作日志,监控内存,垃圾回收等待
setDaemon(true);// 默认是false

并发

  • 同一个对象被多个线程操作

线程同步

  • 多线程访问一个对象需要线程同步,一种等待机制,进入这个对象的等待池形成队列。
  • 队列和锁
同步方法
  • 关键字synchronized
  • synchronized方法和synchronized块

synchronized方法:

public synchronized void method(int[] args){}

示例:

package 线程;

public class SynchronizedTest {
    public static void main(String[] args) {
        Ticks ticks = new Ticks();
        Thread me = new Thread(ticks,"我自己");
        Thread you = new Thread(ticks,"黄牛");
        me.start();
        you.start();

    }

}

class Ticks implements Runnable{
    private int ticks = 10;
    @Override
    public synchronized void run() {

        while (ticks >= 0) {

            System.out.println(Thread.currentThread().getName() + "抢到票了,还剩" + ticks + "张");
            ticks -= 1;
        }

    }
}
  • synchronized块:
synchronized (对象名){语句块};

示例:

package 线程;

public class SynchronizedTest {
    public static void main(String[] args) {
        Ticks ticks = new Ticks();
        Thread me = new Thread(ticks,"我自己");
        Thread you = new Thread(ticks,"黄牛");
        me.start();
        you.start();

    }

}

class Ticks implements Runnable{
    private int ticks = 10;
    @Override
    public void run() {
        synchronized (this) {
            while (ticks >= 0) {

                System.out.println(Thread.currentThread().getName() + "抢到票了,还剩" + ticks + "张");
                ticks -= 1;
            }
        }
    }
}
  • synchronized方法控制对“对象”的访问,每个对象对应一把锁
  • 影响效率
死锁
  • 某一个同步块同时拥有两个以上对象的锁
  • 多个线程互相抱着对方需要的资源,形成僵持
锁(lock)
private final ReentrantLock lock = new ReentrantLock();
try{
    lock.lock();
}
finally{
    lock.unlock();
}

示例:

package 线程;

import java.util.concurrent.locks.ReentrantLock;

public class SynchronizedTest {
    public static void main(String[] args) {
        Ticks ticks = new Ticks();
        Thread me = new Thread(ticks,"我自己");
        Thread you = new Thread(ticks,"黄牛");
        me.start();
        you.start();

    }

}

class Ticks implements Runnable{
    private int ticks = 10;
    private final ReentrantLock lock = new ReentrantLock();
    @Override
    public void run() {
        try{
            lock.lock();
            while (ticks >= 0) {

                System.out.println(Thread.currentThread().getName() + "抢到票了,还剩" + ticks + "张");
                ticks -= 1;
            }
        }
        finally {
            lock.unlock();
        }


    }
}

线程通信

解决方法:

  • 缓冲区
  • 信号灯

Lamda表达式

函数式接口

  • 任何接口,如果只包含唯一一个抽象方法,就是函数式接口
  • 函数式接口,可以通过Lamda表达式创建该对象

使用方法:

public class TestLamda {
    public static void main(String[] args) {
        //正常使用
        Like like = new Like();
        like.pp();
        // 使用lamda表达式
        Me me;
        me = ()->{
            System.out.println("哈哈");
        };
        me.pp();
    }
}

interface Me{
    void pp();
}

class Like implements Me{

    @Override
    public void pp() {
        System.out.println("打印");
    }
}

JDBC

  1. 加载驱动
  2. 连接数据库
  3. 活的执行sql的对象
  4. 获得返回的结果集
  5. 释放连接

代码示例

package JDBC;

import java.sql.*;

public class JdbcTest {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        // 1.加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        // 2.用户信息和url
        String url = "jdbc:mysql://localhost:3306/chapter01?useUnicode=true&characterEncoding=utf8&useSSl=true";
        String username="root";
        String password = "123456";
        // 3.连接数据库
        Connection connection = DriverManager.getConnection(url,username,password);
        // 4.生成执行sql的对象
        Statement statement = connection.createStatement();
        // 5.执行sql语句
        String sql = "SELECT * FROM STUDENT";
        ResultSet resultSet = statement.executeQuery(sql);
        while (resultSet.next()) {
            System.out.println("id:" + resultSet.getObject("sid"));
            System.out.println("sname:" + resultSet.getObject("sname"));
            System.out.println("age:" + resultSet.getObject("age"));
            System.out.println("course:" + resultSet.getObject("course"));
            System.out.println("===================================");
        }
        // 6.释放连接
        resultSet.close();
        statement.close();
        connection.close();
    }
}

网络编程

要素

双方地址:

  • ip
  • 端口号

网络通信协议:UDP,TCP

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值