多线程和String类测试更新

This commit is contained in:
dingjiawen 2022-09-13 19:57:45 +08:00
parent 806e02d386
commit 06d21ded9b
5 changed files with 313 additions and 0 deletions

View File

@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.markilue.java_learning</groupId>
<artifactId>java_learning</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- <dependency>-->
<!-- <groupId>org.projectlombok</groupId>-->
<!-- <artifactId>lombok</artifactId>-->
<!-- <version>RELEASE</version>-->
<!--&lt;!&ndash; <scope>compile</scope>&ndash;&gt;-->
<!-- </dependency>-->
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,112 @@
package com.markilue.java_learning.string;
import org.junit.Test;
/**
* @BelongsProject: java_learning
* @BelongsPackage: com.markilue.java_learning.string
* @Author: dingjiawen
* @CreateTime: 2022-09-13 18:45
* @Description:
* TODO 解释string中的==和equal方法,以及string的intern()方法:
* 1)String类型不是基本类型,其构造方法有String s=;new String()/new String("")/new String(char[] a)
* 2String由于其不是基本类型因此使用构造方法new String(abc)创建对象其对象会放在堆中(对象引用放在栈里),"abc"存放在对象的value数组中(源码可知)
* 3使用String s=abc创建的String,由于abc是一个不可变常量(在常量池中只会维护一份,并且可以共享),因此"abc"会存在常量池中,s的对象引用也是指向常量池
* 4) String调用的equal()方法,本身是调用了Object类的equal()方法比较内存但是String类重写了equal()方法变成了比较值
* 4.1)String的intern()方法就是将string放入常量池中,并返回他的索引,如果在常量池中则直接返回他的索引
* 5)关于拼接和存储问题
* 1常量+常量结果是常量池
* 2常量与变量 变量与变量结果是堆
* 3拼接后调用intern方法结果在常量池
* 4在初始化时使用String s=new String("abc")+new String("def")进行拼接时不会马上解析只有当这个字面量s被调用或者abcdef被调用时,采用创建对应的String实例
* @Version: 1.0
*/
public class Equal {
@Test
public void test1() {
//参考23
String str1 = "hello";
String str2 = "hello";
System.out.println(str1 == str2);//true
String str3 = new String("hello");
String str4 = new String("hello");
System.out.println(str1 == str4); //false
System.out.println(str3 == str4); //false
}
@Test
public void test2() {
//参考5)的第三点
String s1 = "hello";
String s2 = "world";
String s3 = "helloworld";
String s4 = (s1 + "world").intern();//把拼接的结果放到常量池中
String s5 = (s1 + s2).intern();
System.out.println(s3 == s4);//true
System.out.println(s3 == s5);//true
}
@Test
public void test05() {
//参考5的第一点
//用final修饰存放在常量池中
final String s1 = "hello";
final String s2 = "world";
String s3 = "helloworld";
String s4 = s1 + "world";//s4字符串内容也helloworlds1是常量"world"常量常量+ 常量 结果在常量池中
String s5 = s1 + s2;//s5字符串内容也helloworlds1和s2都是常量常量+ 常量 结果在常量池中
String s6 = "hello" + "world";//常量+ 常量 结果在常量池中因为编译期间就可以确定结果
System.out.println(s3 == s4);//true
System.out.println(s3 == s5);//true
System.out.println(s3 == s6);//true
}
@Test
public void test04() {
//参考5的第2点
String s1 = "hello";
String s2 = "world";
String s3 = "helloworld";
String s4 = s1 + "world";//s4字符串内容也helloworlds1是变量"world"常量变量 + 常量的结果在堆中
String s5 = s1 + s2;//s5字符串内容也helloworlds1和s2都是变量变量 + 变量的结果在堆中
String s6 = "hello" + "world";//常量+ 常量 结果在常量池中因为编译期间就可以确定结果
System.out.println(s3 == s4);//false
System.out.println(s3 == s5);//false
System.out.println(s3 == s6);//true
}
@Test
public void test03() {
//参考5的第4点 存疑?现在运行好像是false
String s1 = new String("1");
s1.intern();
String s2 = "1";
System.out.println(s1 == s2);//false
String s3 = new String("1") + new String("1");
s3.intern();
String s4 = "11";
System.out.println(s3 == s4);//true =>现在运行好像是false了?
}
@Test
public void test07() {
//参考5的第4点
String s1 = new String("mark") + new String("ilue"); //这一步会将mark和ilue放入常量池中
s1.intern(); //这里拼接的字符串markilue被调用,将markilue放入常量池中,这时才返回markilue索引给s1
String s2=new StringBuilder("marki").append("lue").toString(); //这里将marki和lue放入常量池中,然后在toString中被调用,所以创建markilue的String对象(存放在堆中)然后返回这个堆索引
System.out.println(s1 == s2);//false 这里一个是返回的在堆对象中的索引一个是返回的在常量池中的索引,因此不相等
System.out.println(s1 == s2.intern());//true 这里调用intern()方法后获取到了在常量池中的索引因此两者相等
}
}

View File

@ -0,0 +1,42 @@
package com.markilue.java_learning.thread;
/**
* @BelongsProject: java_learning
* @BelongsPackage: com.markilue.java_learning.thread
* @Author: dingjiawen
* @CreateTime: 2022-09-13 19:54
* @Description:
* TODO 守护线程:
* 有一种线程它是在后台运行的它的任务是为其他线程提供服务的这种线程被称为守护线程JVM的垃圾回收线程就是典型的守护线程
* 守护线程有个特点就是如果所有非守护线程都死亡那么守护线程自动死亡
* 调用setDaemon(true)方法可将指定线程设置为守护线程必须在线程启动之前设置否则会报IllegalThreadStateException异常
* 调用isDaemon()可以判断线程是否是守护线程
* @Version: 1.0
*/
public class Daemon {
public static void main(String[] args) {
MyDaemon m = new MyDaemon();
m.setDaemon(true);
m.start();
for (int i = 1; i <= 100; i++) {
System.out.println("main:" + i);
}
}
public static class MyDaemon extends Thread {
public void run() {
while (true) {
System.out.println("我一直守护者你...");
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}

View File

@ -0,0 +1,92 @@
package com.markilue.java_learning.thread;
import java.util.Scanner;
/**
* @BelongsProject: java_learning
* @BelongsPackage: com.markilue.java_learning
* @Author: dingjiawen
* @CreateTime: 2022-09-13 18:32
* @Description:
* TODO 多线程尝试强行加塞:
* Thread.sleep方法使得线程沉睡等到了指定时间重新开始执行
* Thread.start方法使得线程开始执行
* Thread,join方法等待该线程终止只有终止程序才会继续往下执行
* @Version: 1.0
*/
public class ThreadDemo1 {
public static void main(String[] args) {
ChatThread t = new ChatThread();
t.start();
for (int i = 1; i < 10; i++) {
System.out.println("main:" + i);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
//当main打印到5之后需要等join进来的线程停止后才会继续了
if(i==5){
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/*
实现的结果:
main:1
main:2
main:3
main:4
main:5
是否结束YN
y
main:6
main:7
main:8
main:9
===========================================
第二次结果:
main:1
main:2
是否结束YN
main:3
main:4
main:5
n
是否结束YN
n
是否结束YN
y
main:6
main:7
main:8
main:9
*/
}
public static class ChatThread extends Thread{
public void run(){
Scanner input = new Scanner(System.in);
while(true){
System.out.println("是否结束Y、N");
char confirm = input.next().charAt(0);
if(confirm == 'Y' || confirm == 'y'){
break;
}
}
input.close();
}
}
}

View File

@ -1,5 +1,7 @@
package com.markilue.leecode.test;
import java.text.SimpleDateFormat;
public class Fibonaqi {