JAVA常用函数
字符串转列表
String ids = "1,2,3,4,5";
List<Integer> idsList = Arrays.stream(ids.split(",")).map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
时间戳转日期格式
Long createdEnd = createTime;/毫秒时间戳
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(simpleDateFormat.format(createTime));
递归删除文件夹
public static void main(String[] args){
File path = new File("/Users/Jeffrey/Desktop/willdelete");
/递归删除
boolean res = deletePath(path);
if(res) System.out.println("success");
}
private static boolean deletePath(File path) {
if (path.exists()){
File[] files = path.listFiles();
assert files != null;
for (File file : files) {
if (file.isFile()){
/直接删除
file.delete();
}else{
/如果是文件夹 继续打开文件夹,判断有没有文件
deletePath(file);
}
}
return path.delete();
}
return true;
}
文件写入
fileOutputStream(byte,boolean append)
fileOutputStream(byre[],boolean append)
/换行 "\r\n".getBytes()
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream("/Users/Jeffrey/Desktop/TTT.txt");
fileOutputStream.write(99);
fileOutputStream.close();
}catch (Exception e){
e.printStackTrace();
}finally {
if (fileOutputStream != null){
try {
fileOutputStream.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
并行和并发、进程和线程
第一种开启线程
public class MyThread extends Thread{
private String title;
public MyThread(String title) {
this.title = title;
}
@Override
public void run(){
for (int i = 0; i < 100; i++) {
System.out.println(this.title + "开启了线程" + i);
}
}
}
/开启线程
MyThread myThread = new MyThread("Test1");
MyThread myThread1 = new MyThread("Test2");
myThread.start();
myThread1.start();
第二种开启线程【Runnable】
public class MyRunnable implements Runnable{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("第二种方式开启线程 " + i);
}
}
}
/开启线程
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
thread.start();
第三种看起线程【Callable和Future】
- 优点
- 扩展性强,实现该接口的同时可以继承其他的类
- 缺点
- 编程相对复杂,不能直接使用Thread类
public class MyCallable implements Callable<String> {
@Override
public String call() throws Exception {
//返回值表示线程执行结束之后的返回结果
for (int i = 0; i < 100; i++) {
System.out.println("第三种开启下线程 " + (i + 1));
}
return "执行结束";
}
}
//开启线程
MyCallable myCallable = new MyCallable();
FutureTask<String> stringFutureTask = new FutureTask<>(myCallable);
Thread thread1 = new Thread(stringFutureTask);
thread1.start();
String s = stringFutureTask.get();
System.out.println(s);
生产者、消费者
消费者
package cn.zhangfayuan.collection;
public class Eater extends Thread{
@Override
public void run() {
while (true){
synchronized (TableDesk.lock){
if (TableDesk.count == 0){
break;
}else{
if (TableDesk.flag){
System.out.println("消费者消费");
TableDesk.flag = false;
TableDesk.lock.notifyAll();
TableDesk.count--;
}else{
try {
TableDesk.lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
}
生产者
package cn.zhangfayuan.collection;
public class Cooker extends Thread{
@Override
public void run() {
while (true){
synchronized (TableDesk.lock){
if (TableDesk.count == 0){
break;
} else {
if (TableDesk.flag){
//有数据 就等待
try {
TableDesk.lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}else{
//生产
System.out.println("生产者生产");
TableDesk.flag = true;
TableDesk.lock.notifyAll();
}
}
}
}
}
}
中间作用类
package cn.zhangfayuan.collection;
public class TableDesk {
public static int count = 10;
public static boolean flag = false;
public static final Object lock = new Object();
}
调用
Eater eater = new Eater();
Cooker cooker = new Cooker();
eater.start();
cooker.start();
//====================== 处理结果 ==================
//生产者生产
//消费者消费
//生产者生产
//消费者消费
//生产者生产
//消费者消费
//生产者生产
//消费者消费
//生产者生产
//消费者消费
//生产者生产
//消费者消费
//生产者生产
//消费者消费
//生产者生产
//消费者消费
//生产者生产
//消费者消费
//生产者生产
//消费者消费
redis-cli -h redis-k268b7ibip3s-proxy-nlb.jvessel-open-hb.jdcloud.com -p 6379 -a 4jq1QdugammK
zip
持枪
1.png
2.png
3.png
持棍
1.png
4.png
5.png
持刀
1.png
6.png
7.png
第一步: /pc/project/task/upload/103 参数 【 id 文件】
第二步 :/pc/project/task/getProjectTaskAllot 参数 【{“projectId”:103,“taskIds”:[270]}】
/pc/project/task/page
不做分类 其他 其他-背包 其他-行为 持刀 持改锥 持枪 持棍 未背包 空手 背包
文档更新时间: 2023-12-18 03:25 作者:JeffreyCheung