===============
== ffff65535 ==
===============
十六个一(1111 1111 1111 1111)

windows使用指定版本jdk启动jar包

jdk jar windows

windows server中已经配置了一个1.8版本的jdk

现在有一个使用jdk17的jar也要运行

CMD执行报错

C:\test>C:\Program Files\Java\openjdk-17.0.2_windows-x64_bin\jdk-17.0.2\bin\java.exe -jar test-0.0.1-SNAPSHOT.jar
'C:\Program' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

原因是Program Files中间有一个空格,给java.exe的整个路径加上双引号

阅读全文...

nextcloud配置缓存

nextcloud 缓存 redis

/config/config.php

'memcache.local' => '\OC\Memcache\APCu',
'memcache.locking' => '\OC\Memcache\Redis',
'redis' => [
  'host' => '127.0.0.1',
  'port' => 6379,
],

参考文档

https://docs.nextcloud.com/server/14/admin_manual/configuration_server/caching_configuration.html

小型/私人家庭服务器

仅使用 APCu:

'memcache.local'  =>  '\OC\Memcache\APCu' ,

小型组织,单服务器设置

使用 APCu 进行本地缓存,使用 Redis 进行文件锁定:

'memcache.local' => '\OC\Memcache\APCu',
'memcache.locking' => '\OC\Memcache\Redis',
'redis' => array(
	'host' => 'localhost',
	'port' => 6379,
),

大型组织,集群设置

对除本地 memcache 之外的所有内容使用 Redis:

阅读全文...

线程池

多线程 线程池 executor

参考资料:

https://www.bilibili.com/video/BV1V4411p7EF?p=27

https://blog.csdn.net/lxy13263050001/article/details/122723983


  • 背景:经常创建和销毁、使用量特别大的资源,比如并发情况下的线程,对性能影响很大
  • 思路:提前创建了好多个线程,放入线程池中,使用时直接获取,使用完放回池中。可以避免频繁创建销毁、实现重复利用。类似生活中的公共交通工具
  • 好处:
    • 提高响应速度(减少了创建新线程的时间)
    • 降低资源消耗(重复利用线程池中线程,不需要每次都创建)
    • 便于线程管理
      • corePoolSize:核心池的大小
      • maximumPoolSize:最大线程数
      • keepAliveTime:线程没有任务是最多保持多长时间后会终止
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author
 */
public class TestPool {
    public static void main(String[] args) {
        // 创建有10个线程的线程池
        ExecutorService threadPool = Executors.newFixedThreadPool(10);
        threadPool.execute(new TestMyThread());
        threadPool.execute(new TestMyThread());
        threadPool.execute(new TestMyThread());
        threadPool.execute(new TestMyThread());
        // 关闭线程池
        threadPool.shutdown();
    }
}

class TestMyThread implements Runnable {

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName() + "----->" + i);
        }
    }
}
  • JDK5.0起提供了线程池相关API:ExecutorService和Executors
  • ExecutorService:真正的线程池接口。常见子类ThreadPoolExecutor
    • void execute(Runnable command) :执行任务/命令,没有返回值,一般用来执行Runnable
    • Future submit(Callable task):执行任务,有返回值,一般又来执行Callable
    • void shutdown() :关闭连接池
  • Executors:工具类、线程池的工厂类,用于创建并返回不同类型的线程池
    • Executors.newCachedThreadPool():创建一个可根据需要创建新线程的线程池
    • Executors.newFixedThreadPool(n); 创建一个可重用固定线程数的线程池
    • Executors.newSingleThreadExecutor() :创建一个只有一个线程的线程池
    • Executors.newScheduledThreadPool(n):创建一个线程池,它可安排在给定延迟后运行命令或者定期地执行

并发协作模型(生产者/消费者模式)-->信号灯法

并发 生产者-消费者 信号量 多线程
方法名作用
wait()Object wait() 方法让当前线程进入等待状态。直到其他线程调用此对象的 notify() 方法或 notifyAll() 方法。
wait(long timeout)Object wait(long timeout) 方法让当前线程处于等待(阻塞)状态,直到其他线程调用此对象的 notify() 方法或 notifyAll() 方法,或者超过参数 timeout 设置的超时时间。
如果 timeout 参数为 0,则不会超时,会一直进行等待,类似于 wait() 方法。
notify()Object notify() 方法用于唤醒一个在此对象监视器上等待的线程。
如果所有的线程都在此对象上等待,那么只会选择一个线程,选择是任意性的,并在对实现做出决定时发生。
notifyAll()Object notifyAll() 方法用于唤醒在该对象上等待的所有线程。优先级别高的线程有限调度。
notifyAll() 方法跟 notify() 方法一样,区别在于 notifyAll() 方法唤醒在此对象监视器上等待的所有线程,notify() 方法是一个线程。

https://www.runoob.com/java/java-object-wait.html

阅读全文...

并发协作模型(生产者/消费者模式)-->管程法

并发 生产者-消费者 管程 多线程
方法名作用
wait()Object wait() 方法让当前线程进入等待状态。直到其他线程调用此对象的 notify() 方法或 notifyAll() 方法。
wait(long timeout)Object wait(long timeout) 方法让当前线程处于等待(阻塞)状态,直到其他线程调用此对象的 notify() 方法或 notifyAll() 方法,或者超过参数 timeout 设置的超时时间。
如果 timeout 参数为 0,则不会超时,会一直进行等待,类似于 wait() 方法。
notify()Object notify() 方法用于唤醒一个在此对象监视器上等待的线程。
如果所有的线程都在此对象上等待,那么只会选择一个线程,选择是任意性的,并在对实现做出决定时发生。
notifyAll()Object notifyAll() 方法用于唤醒在该对象上等待的所有线程。优先级别高的线程有限调度。
notifyAll() 方法跟 notify() 方法一样,区别在于 notifyAll() 方法唤醒在此对象监视器上等待的所有线程,notify() 方法是一个线程。

https://www.runoob.com/java/java-object-wait.html

阅读全文...

自定义校验注解ConstraintValidator

bean-validation 枚举校验 constraint-validator

新建一个枚举类,限制请求参数中的属性值只能等于枚举中的值

public enum EnumType {
    /**
     * 数组中是要等于的值每增加一个要校验的类型,
     * 增加一个对应枚举类
     */
    // 性别
    GENDER_TYPE(new String[]{"0", "1"}),
    // 年级
    GRADE_TYPE(new String[]{"1", "2", "3"});

    private String[] values;

    EnumType(String[] values) {
        this.values = values;
    }

    public String[] getValues() {
        return values;
    }
}

新建一个自定义注解

阅读全文...

守护(daemon)线程

多线程 守护线程
  • 线程分为用户线程和守护线程
  • 虚拟机必须确保用户线程执行完毕
  • 虚拟机不用等待守护线程执行完毕
    • 如:后台记录操作日志,监控内存,垃圾回收等待…
public class TestThreadDaemon {
    public static void main(String[] args) {
        God god = new God();
        Thread godThread = new Thread(god);
        // 默认是false,表示用户线程
        godThread.setDaemon(true);
        godThread.start();

        // 你 用户线程启动
        new Thread(new You()).start();

        // 用户线程执行完毕,守护线程也会停止
    }
}

// 上帝 守护线程
class God implements Runnable {

    @Override
    public void run() {
        while (true) {
            System.out.println("上帝保佑着你");
        }
    }
}

// 你 用户线程
class You implements Runnable {
    @Override
    public void run() {
        System.out.println("=======hello world=======");
        for (int i = 0; i < 35600; i++) {
            System.out.println("你一生都开心活着");
        }
        System.out.println("=======goodbye world=======");
    }
}

参考资料:

阅读全文...

线程优先级

多线程 线程优先级
  • Java提供一个线程调度器来监控程序中启动后进入就绪状态的所有线程,线程调度器按照优先级决定应该调度哪个线程来执行
  • 线程优先级用数字表示,范围从1~10(线程的调度由CPU决定,数字越大概率越大)
    • Thread.MIN_PRIORITY = 1
    • Thread.NORM_PRIORITY = 5
    • Thread.MAX_PRIORITY = 10
  • 使用以下方法改变或获取线程优先级
    • getPriority()
    • setPriority(int xxx)
public class TestThreadPriority {
    public static void main(String[] args) {
        // 打印主线程默认优先级
        System.out.println(Thread.currentThread().getName() + "====" + Thread.currentThread().getPriority());

        Priority priority = new Priority();

        Thread t1 = new Thread(priority, "t1");
        Thread t2 = new Thread(priority, "t2");
        Thread t3 = new Thread(priority, "t3");
        Thread t4 = new Thread(priority, "t4");
        Thread t5 = new Thread(priority, "t5");
        Thread t6 = new Thread(priority, "t6");

        t1.start();
        t2.setPriority(1);
        t2.start();
        t3.setPriority(4);
        t3.start();
        t4.setPriority(Thread.MAX_PRIORITY);
        t4.start();
        t5.setPriority(6);
        t5.start();
        t6.setPriority(8);
        t6.start();
    }
}

class Priority implements Runnable {
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "====" + Thread.currentThread().getPriority());
    }
}

参考资料:

阅读全文...

观测线程状态

多线程 线程状态

线程状态

  • NEW
  • 尚未启动的线程处于此状态
  • RUNNABLE
  • 在java虚拟机中执行的线程处于此状态
  • BLOCKED
  • 被阻塞等待监听器锁定的线程处于此状态
  • WAITING
  • 正在等待另一个线程执行特定动作的线程处于此状态
  • TIMED_WAITING
  • 正在等待另一个线程执行动作到达指定等待时间的线程处于此状态
  • TERMINATED
  • 已退出的线程处于此状态
public enum State {
    NEW,
    RUNNABLE,
    BLOCKED,
    WAITING,
    TIMED_WAITING,
    TERMINATED;
}
/**
 *观察测试线程的状态
*/
public class TestThreadState {
    public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("////////");
            }
        }, "观测线程");
        // 观察状态 NEW
        System.out.println(thread.getState());

        // 观察启动后
        thread.start();
        // RUNNABLE
        System.out.println(thread.getState());

        // 线程不终止一直输出状态
        while (thread.getState() != Thread.State.TERMINATED) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // TIMED_WAITING
            System.out.println(thread.getState());
        }
        //TERMINATED
        System.out.println(thread.getState());
    }
}

参考资料:

阅读全文...

lambda表达式

lambda 函数式接口

函数式接口(Functional Interface)就是一个有且仅有一个抽象方法,但是可以有多个非抽象方法的接口。

函数式接口可以被隐式转换为 lambda 表达式。

阅读全文...
Previous Page 3 of 4 Next Page