Java_note_1
Java_note Basis
1.Collection_Class
ArrayList_Class
ArrayList_Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package note_file.Basis.Collection_Class;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
public class List_demo1 {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
/*
* List特点:[感觉就是队列]
* 1.有序:存储和取出元素顺序相同
* 2.可重复:存储的元素可以重复
*/
list.add("hello");
list.add("world");
list.add("java");
list.add("world");
list.add(3, "NBA champion");// 在index处插入element
list.remove(2);
list.remove("hello");
list.set(2, "Stephen Curry");// 修改index处的值为element
// 遍历
// 方法一
Iterator<String> it = list.iterator();
while (it.hasNext()) {
String s = it.next();
/*
* if (s.equals("world")) {// 此代码会引起并发修改异常
* list.add("JDK");
* }
*/
// 迭代器运行过程中代码不能修改集合的长度,否则就会出现并发修改异常
System.out.println(s);
}
// 方法二
for (int i = 0; i < list.size(); i++) {
String s = list.get(i);
if (s.equals("world")) {
list.add("JDK");
}
System.out.println(list.get(i));
} // 这种遍历方式可以做如上操作
System.out.println(list);
// listIterator
ListIterator<String> lit = list.listIterator();
while (lit.hasNext()) {
String s = lit.next();
if (s.equals("world")) {
lit.add("Java");
}
System.out.println(s);
} // 这种方法不会出现并行修改异常
while (lit.hasPrevious()) {
String s = lit.previous();
System.out.println(s);
}
// 有点鸡肋,必须先向后才能向前
// 增强for循环
for (String s : list) {
System.out.println(s);
} // 本质是Iterator迭代器
/*
* ArrayList底层数据结构为数组
* LinkedList底层数据结构为链表
*/
LinkedList<String> linked_list = new LinkedList<String>();
linked_list.add("hello");
linked_list.add("world");
linked_list.add("java");
// LinkedList遍历
Iterator<String> linked_it = linked_list.iterator();
while (linked_it.hasNext()) {
System.out.println(linked_it.next());
}
for (String s : linked_list) {
System.out.println(s);
}
// LinkedList的特有操作
linked_list.addFirst("NBA");// 头结点操作
linked_list.addLast("Curry");// 尾结点操作
System.out.println(linked_list.removeFirst());// 删除头结点并返回
System.out.println(linked_list.removeLast());// 删除尾结点并返回
System.out.println(linked_list.getFirst());// 获得第一个元素
System.out.println(linked_list.getLast());// 获得尾元素
System.out.println(linked_list.getClass());// 获得类[这个不是特有]
}
}
Collection_demo1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package note_file.Basis.Collection_Class;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Collection_demo1 {
public static void main(String[] args) {
// 创建Collection集合对象
Collection<String> c = new ArrayList<>();// 多态构造
// 添加元素
c.add("hello");
c.add("world");
c.add("java");
System.out.println(c);
// 删除元素
if (c.contains("hello")) {// 判断是否存在指定元素
c.remove("hello");
}
// 遍历
// 方法一
for (int i = 0; i < c.size(); i++) {
System.out.println(((ArrayList<String>) c).get(i));// 向下转型,注意用ArrayList<>时要加类型
}
// 方法二
Iterator<String> it = c.iterator();// 迭代器
while (it.hasNext()) {
String s = it.next();
System.out.println(s);
}
if (!c.isEmpty()) {// 判断是否为空集合
c.clear();// 清空
}
}
}
Collections_Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package note_file.Basis.Collection_Class;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Collections_Class {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(30);
list.add(50);
list.add(20);
list.add(10);
list.add(40);
Collections.sort(list);// 排序
Collections.reverse(list);// 反转
Collections.shuffle(list);// 随机置换[类似洗牌]
System.out.println(list);
}
}
List_demo1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package note_file.Basis.Collection_Class;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
public class List_demo1 {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
/*
* List特点:[感觉就是队列]
* 1.有序:存储和取出元素顺序相同
* 2.可重复:存储的元素可以重复
*/
list.add("hello");
list.add("world");
list.add("java");
list.add("world");
list.add(3, "NBA champion");// 在index处插入element
list.remove(2);
list.remove("hello");
list.set(2, "Stephen Curry");// 修改index处的值为element
// 遍历
// 方法一
Iterator<String> it = list.iterator();
while (it.hasNext()) {
String s = it.next();
/*
* if (s.equals("world")) {// 此代码会引起并发修改异常
* list.add("JDK");
* }
*/
// 迭代器运行过程中代码不能修改集合的长度,否则就会出现并发修改异常
System.out.println(s);
}
// 方法二
for (int i = 0; i < list.size(); i++) {
String s = list.get(i);
if (s.equals("world")) {
list.add("JDK");
}
System.out.println(list.get(i));
} // 这种遍历方式可以做如上操作
System.out.println(list);
// listIterator
ListIterator<String> lit = list.listIterator();
while (lit.hasNext()) {
String s = lit.next();
if (s.equals("world")) {
lit.add("Java");
}
System.out.println(s);
} // 这种方法不会出现并行修改异常
while (lit.hasPrevious()) {
String s = lit.previous();
System.out.println(s);
}
// 有点鸡肋,必须先向后才能向前
// 增强for循环
for (String s : list) {
System.out.println(s);
} // 本质是Iterator迭代器
/*
* ArrayList底层数据结构为数组
* LinkedList底层数据结构为链表
*/
LinkedList<String> linked_list = new LinkedList<String>();
linked_list.add("hello");
linked_list.add("world");
linked_list.add("java");
// LinkedList遍历
Iterator<String> linked_it = linked_list.iterator();
while (linked_it.hasNext()) {
System.out.println(linked_it.next());
}
for (String s : linked_list) {
System.out.println(s);
}
// LinkedList的特有操作
linked_list.addFirst("NBA");// 头结点操作
linked_list.addLast("Curry");// 尾结点操作
System.out.println(linked_list.removeFirst());// 删除头结点并返回
System.out.println(linked_list.removeLast());// 删除尾结点并返回
System.out.println(linked_list.getFirst());// 获得第一个元素
System.out.println(linked_list.getLast());// 获得尾元素
System.out.println(linked_list.getClass());// 获得类[这个不是特有]
}
}
1 | package note_file.Basis.Collection_Class; |
1 | package note_file.Basis.Collection_Class; |
Collections_Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package note_file.Basis.Collection_Class;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Collections_Class {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(30);
list.add(50);
list.add(20);
list.add(10);
list.add(40);
Collections.sort(list);// 排序
Collections.reverse(list);// 反转
Collections.shuffle(list);// 随机置换[类似洗牌]
System.out.println(list);
}
}
List_demo1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package note_file.Basis.Collection_Class;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
public class List_demo1 {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
/*
* List特点:[感觉就是队列]
* 1.有序:存储和取出元素顺序相同
* 2.可重复:存储的元素可以重复
*/
list.add("hello");
list.add("world");
list.add("java");
list.add("world");
list.add(3, "NBA champion");// 在index处插入element
list.remove(2);
list.remove("hello");
list.set(2, "Stephen Curry");// 修改index处的值为element
// 遍历
// 方法一
Iterator<String> it = list.iterator();
while (it.hasNext()) {
String s = it.next();
/*
* if (s.equals("world")) {// 此代码会引起并发修改异常
* list.add("JDK");
* }
*/
// 迭代器运行过程中代码不能修改集合的长度,否则就会出现并发修改异常
System.out.println(s);
}
// 方法二
for (int i = 0; i < list.size(); i++) {
String s = list.get(i);
if (s.equals("world")) {
list.add("JDK");
}
System.out.println(list.get(i));
} // 这种遍历方式可以做如上操作
System.out.println(list);
// listIterator
ListIterator<String> lit = list.listIterator();
while (lit.hasNext()) {
String s = lit.next();
if (s.equals("world")) {
lit.add("Java");
}
System.out.println(s);
} // 这种方法不会出现并行修改异常
while (lit.hasPrevious()) {
String s = lit.previous();
System.out.println(s);
}
// 有点鸡肋,必须先向后才能向前
// 增强for循环
for (String s : list) {
System.out.println(s);
} // 本质是Iterator迭代器
/*
* ArrayList底层数据结构为数组
* LinkedList底层数据结构为链表
*/
LinkedList<String> linked_list = new LinkedList<String>();
linked_list.add("hello");
linked_list.add("world");
linked_list.add("java");
// LinkedList遍历
Iterator<String> linked_it = linked_list.iterator();
while (linked_it.hasNext()) {
System.out.println(linked_it.next());
}
for (String s : linked_list) {
System.out.println(s);
}
// LinkedList的特有操作
linked_list.addFirst("NBA");// 头结点操作
linked_list.addLast("Curry");// 尾结点操作
System.out.println(linked_list.removeFirst());// 删除头结点并返回
System.out.println(linked_list.removeLast());// 删除尾结点并返回
System.out.println(linked_list.getFirst());// 获得第一个元素
System.out.println(linked_list.getLast());// 获得尾元素
System.out.println(linked_list.getClass());// 获得类[这个不是特有]
}
}
1 | package note_file.Basis.Collection_Class; |
1 | package note_file.Basis.Collection_Class; |
2.Data_Class
Calendar_Class
Calendar_Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package note_file.Basis.Date_Class;
import java.util.Calendar;
public class Calendar_Class {
public static void main(String[] args) {
// 获得对象
Calendar c = Calendar.getInstance();// 通过源代码发现是通过多态创造的对象
print_date(c);
c.add(Calendar.YEAR, -3);// 减去3年
c.add(Calendar.MONTH, 6);
print_date(c);
c.set(Calendar.YEAR, 2022);
print_date(c);
c.set(2012, 11, 27);// 注意print_date时会把月加一,故输出12月要设置为11月
print_date(c);
}
public static void print_date(Calendar c) {
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;// month从0开始,所以要加一
int date = c.get(Calendar.DATE);
System.out.println(year + "年" + month + "月" + date + "日");
}
}
Date_Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package note_file.Basis.Date_Class;
import java.util.Date;
//Date在多个包内均含有,本文档学习的是util包下的
public class Date_Class {
public static void main(String[] args) {
Date d1 = new Date();
System.out.println(d1);
long date = 1000 * 60 * 60;// 一小时
Date d2 = new Date(date);
System.out.println(d2);
// 从1970.01.01开始计算
System.out.println(d1.getTime() * 1.0 / 1000 / 60 / 60 / 24 / 365 + "年");// 获取时间差
d1.setTime(date);// 设置时间
System.out.println(d1);
}
}
SimpleDateFormat_Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package note_file.Basis.Date_Class;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFormat_Class {
public static void main(String[] args) throws ParseException {// throws部分之后讲解
// 1.Date->String
Date d = new Date();
SimpleDateFormat sdf_1 = new SimpleDateFormat();
String s1 = sdf_1.format(d);
System.out.println(s1);// 默认
SimpleDateFormat sdf_2 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// 注意大小写
String s2 = sdf_2.format(d);
System.out.println(s2);// 自定义格式化
// 2.String->Date
String s3 = "2022年07月03日 20:55:12";// 注意字符串要和定义完全一一对应
Date dd = sdf_2.parse(s3);
System.out.println(dd);
}
}
1 | package note_file.Basis.Date_Class; |
1 | package note_file.Basis.Date_Class; |
SimpleDateFormat_Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package note_file.Basis.Date_Class;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFormat_Class {
public static void main(String[] args) throws ParseException {// throws部分之后讲解
// 1.Date->String
Date d = new Date();
SimpleDateFormat sdf_1 = new SimpleDateFormat();
String s1 = sdf_1.format(d);
System.out.println(s1);// 默认
SimpleDateFormat sdf_2 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// 注意大小写
String s2 = sdf_2.format(d);
System.out.println(s2);// 自定义格式化
// 2.String->Date
String s3 = "2022年07月03日 20:55:12";// 注意字符串要和定义完全一一对应
Date dd = sdf_2.parse(s3);
System.out.println(dd);
}
}
1 | package note_file.Basis.Date_Class; |
3.Map_Class
Map_Class
1 | package note_file.Basis.Map_Class; |
4.Object_Class
Object_Class
object_Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package note_file.Basis.Object_Class;
public class Object_Class {
public static void main(String[] args) {
Student s1 = new Student();
s1.setName("charles");
s1.setAge(19);
System.out.println(s1);// note_file.Basis.Object_Class.Student@28a418fc
/*
* 重写前为:
* public String toString() {
* return getClass().getName() + "@" + Integer.toHexString(hashCode());
* }
*/
System.out.println(s1.toString());// 选中后按F3可以看到源代码
Student s2 = new Student();
s2.setName("Lily");
s2.setAge(43);
// System.out.println(s1 == s2);[这种方式是比较地址是否相同]
System.out.println(s1.equals(s2));// 这种方式是比较内容是否相同,但需要重写,若不重写和上面一行代码作用相同
/*
* 源码:
* public boolean equals(Object obj) {
* return (this == obj);
* }
*/
}
}
student.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package note_file.Basis.Object_Class;
public class Student {
// 默认继承Object方法
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "Student [age=" + age + ", name=" + name + "]";
}// 建议子类重写toString方法,利用Alt+Shift+S后按toString(),即可重写
public boolean equals(Object obj) {
if (this == obj)
// 若地址相同,直接返回true
return true;
if (obj == null)
// 判断参数是否为null
return false;
if (getClass() != obj.getClass())
// 判断两个对象是否来自同一个类
return false;
Student other = (Student) obj;// 向下转型
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}// 建议子类重写equals方法,同时生成了hashCode(),删除即可
}
1 | package note_file.Basis.Object_Class; |
1 | package note_file.Basis.Object_Class; |
5.Set_Class
Set_Class
Hash_demo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
package note_file.Basis.Set_Class;
public class Hash_demo {
public static void main(String[] args) {
Student_hash s1 = new Student_hash("charles", 19);
Student_hash s2 = new Student_hash("Lily", 43);
// 同一对象多次调用hashcode()方法返回的hash值相同
System.out.println(s1.hashCode());
System.out.println(s2.hashCode());
// 默认情况下,不同对象的hash值不同
}
}
HashSet_Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package note_file.Basis.Set_Class;
/*
* HashSet集合特点:
* 1.底层数据结构是哈希表
* 2.对集合的迭代顺序不作任何保证,元素存储和取出的顺序不一定一致
* 3.没有带索引的方法,所以不能使用for循环遍历
* 4.由于是Set集合,所以不能含有重复元素
* 5.HashSet集合存储元素,要保证元素的唯一性,需要重写hashCode()和equals()
*/
import java.util.HashSet;
public class HashSet_Class {
public static void main(String[] args) {
HashSet<String> hs = new HashSet<>();
hs.add("hello");
hs.add("world");
hs.add("java");
// 遍历
for (String s : hs) {
System.out.println(s);
}
// 无重复元素
hs.add("java");
System.out.println(hs);// 未按顺序
}
}
LinkedHashSet_Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package note_file.Basis.Set_Class;
import java.util.LinkedHashSet;
/*
* LinkedHashSet特点:
* 1.hash表和链表实现的Set接口,具有可预测的迭代次序
* 2.由链表保证元素有序,也就是说元素的存储和取出顺序一致
* 3.由hash表保证元素唯一,也就是说没有重复元素
*/
public class LinkedHashSet_Class {
public static void main(String[] args) {
LinkedHashSet<String> lhs = new LinkedHashSet<>();
lhs.add("hello");
lhs.add("world");
lhs.add("java");
for (String s : lhs) {
System.out.println(s);
}
lhs.add("world");
System.out.println(lhs);
}
}
Set_demo1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package note_file.Basis.Set_Class;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class Set_demo1 {
/*
* Set特点:
* 1.Set是不包含重复元素的集合
* 2.没有带索引的方法,所以不能使用for循环遍历
* 3.Set是一个接口,不能直接建立对象
* HashSet
*/
public static void main(String[] args) {
Set<String> set = new HashSet<String>();
set.add("hello");
set.add("world");
set.add("java");
// 两种遍历方法
for (String s : set) {
System.out.println(s);
}
Iterator<String> it = set.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
// 不包含重复元素
set.add("world");
System.out.println(set);
}
}
Student_hash.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package note_file.Basis.Set_Class;
public class Student_hash implements Comparable<Student_hash> {
private String name;
private int age;
public Student_hash() {
}
public Student_hash(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student_hash other = (Student_hash) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
// 排序时需要在类中连接Comparable接口并重写如下方法compareTo
public int compareTo(Student_hash o) {
int num = this.age - o.age;
// 升序就把this放前面,降序就把this放后面
int num2 = num == 0 ? this.name.compareTo(o.name) : num;// String中自带compareTo以实现字母序
return num2;
}
}
TreeSet_Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package note_file.Basis.Set_Class;
import java.util.Comparator;
/*
* TreeSet集合特点:
* 1.元素有序,具体排序方式取决于构造方法
* 2.没有带索引的方法,所以不能使用普通for循环遍历
* 3.由于是Set集合,所以不包含重复元素
*/
import java.util.TreeSet;
public class TreeSet_Class {
public static void main(String[] args) {
TreeSet<Integer> ts = new TreeSet<>();// 存储类型为对应的包装类型
ts.add(10);// 自动装箱
ts.add(40);
ts.add(30);
ts.add(50);
ts.add(30);// 不包含重复元素
for (Integer i : ts) {
System.out.println(i);
} // 自动排序
// 创建学生集合并按年龄从小到大排序,年龄相同时按字母序
TreeSet<Student_hash> t = new TreeSet<>(new Comparator<Student_hash>() {
public int compare(Student_hash o1, Student_hash o2) {
int num = o1.getAge() - o2.getAge();
int num2 = num == 0 ? o1.getName().compareTo(o2.getName()) : num;// String中自带compareTo以实现字母序
return num2;
}
});// 类似快排的cmp,但实际底层数据结构是hash加链表
// 这里可以用Comparator构造排序规则,也可以在Student_hash类中连接Comparable接口并重写规则
Student_hash s1 = new Student_hash("charles", 19);
Student_hash s2 = new Student_hash("Lily", 43);
Student_hash s3 = new Student_hash("angel", 17);
Student_hash s4 = new Student_hash("Lily", 43);
t.add(s1);
t.add(s2);
t.add(s3);
t.add(s4);
for (Student_hash i : t) {
System.out.println(i.getName() + " " + i.getAge());
}
}
}
1 | package note_file.Basis.Set_Class; |
1 | package note_file.Basis.Set_Class; |
LinkedHashSet_Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package note_file.Basis.Set_Class;
import java.util.LinkedHashSet;
/*
* LinkedHashSet特点:
* 1.hash表和链表实现的Set接口,具有可预测的迭代次序
* 2.由链表保证元素有序,也就是说元素的存储和取出顺序一致
* 3.由hash表保证元素唯一,也就是说没有重复元素
*/
public class LinkedHashSet_Class {
public static void main(String[] args) {
LinkedHashSet<String> lhs = new LinkedHashSet<>();
lhs.add("hello");
lhs.add("world");
lhs.add("java");
for (String s : lhs) {
System.out.println(s);
}
lhs.add("world");
System.out.println(lhs);
}
}
Set_demo1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package note_file.Basis.Set_Class;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class Set_demo1 {
/*
* Set特点:
* 1.Set是不包含重复元素的集合
* 2.没有带索引的方法,所以不能使用for循环遍历
* 3.Set是一个接口,不能直接建立对象
* HashSet
*/
public static void main(String[] args) {
Set<String> set = new HashSet<String>();
set.add("hello");
set.add("world");
set.add("java");
// 两种遍历方法
for (String s : set) {
System.out.println(s);
}
Iterator<String> it = set.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
// 不包含重复元素
set.add("world");
System.out.println(set);
}
}
Student_hash.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package note_file.Basis.Set_Class;
public class Student_hash implements Comparable<Student_hash> {
private String name;
private int age;
public Student_hash() {
}
public Student_hash(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student_hash other = (Student_hash) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
// 排序时需要在类中连接Comparable接口并重写如下方法compareTo
public int compareTo(Student_hash o) {
int num = this.age - o.age;
// 升序就把this放前面,降序就把this放后面
int num2 = num == 0 ? this.name.compareTo(o.name) : num;// String中自带compareTo以实现字母序
return num2;
}
}
TreeSet_Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package note_file.Basis.Set_Class;
import java.util.Comparator;
/*
* TreeSet集合特点:
* 1.元素有序,具体排序方式取决于构造方法
* 2.没有带索引的方法,所以不能使用普通for循环遍历
* 3.由于是Set集合,所以不包含重复元素
*/
import java.util.TreeSet;
public class TreeSet_Class {
public static void main(String[] args) {
TreeSet<Integer> ts = new TreeSet<>();// 存储类型为对应的包装类型
ts.add(10);// 自动装箱
ts.add(40);
ts.add(30);
ts.add(50);
ts.add(30);// 不包含重复元素
for (Integer i : ts) {
System.out.println(i);
} // 自动排序
// 创建学生集合并按年龄从小到大排序,年龄相同时按字母序
TreeSet<Student_hash> t = new TreeSet<>(new Comparator<Student_hash>() {
public int compare(Student_hash o1, Student_hash o2) {
int num = o1.getAge() - o2.getAge();
int num2 = num == 0 ? o1.getName().compareTo(o2.getName()) : num;// String中自带compareTo以实现字母序
return num2;
}
});// 类似快排的cmp,但实际底层数据结构是hash加链表
// 这里可以用Comparator构造排序规则,也可以在Student_hash类中连接Comparable接口并重写规则
Student_hash s1 = new Student_hash("charles", 19);
Student_hash s2 = new Student_hash("Lily", 43);
Student_hash s3 = new Student_hash("angel", 17);
Student_hash s4 = new Student_hash("Lily", 43);
t.add(s1);
t.add(s2);
t.add(s3);
t.add(s4);
for (Student_hash i : t) {
System.out.println(i.getName() + " " + i.getAge());
}
}
}
1 | package note_file.Basis.Set_Class; |
1 | package note_file.Basis.Set_Class; |
Student_hash.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package note_file.Basis.Set_Class;
public class Student_hash implements Comparable<Student_hash> {
private String name;
private int age;
public Student_hash() {
}
public Student_hash(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student_hash other = (Student_hash) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
// 排序时需要在类中连接Comparable接口并重写如下方法compareTo
public int compareTo(Student_hash o) {
int num = this.age - o.age;
// 升序就把this放前面,降序就把this放后面
int num2 = num == 0 ? this.name.compareTo(o.name) : num;// String中自带compareTo以实现字母序
return num2;
}
}
TreeSet_Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package note_file.Basis.Set_Class;
import java.util.Comparator;
/*
* TreeSet集合特点:
* 1.元素有序,具体排序方式取决于构造方法
* 2.没有带索引的方法,所以不能使用普通for循环遍历
* 3.由于是Set集合,所以不包含重复元素
*/
import java.util.TreeSet;
public class TreeSet_Class {
public static void main(String[] args) {
TreeSet<Integer> ts = new TreeSet<>();// 存储类型为对应的包装类型
ts.add(10);// 自动装箱
ts.add(40);
ts.add(30);
ts.add(50);
ts.add(30);// 不包含重复元素
for (Integer i : ts) {
System.out.println(i);
} // 自动排序
// 创建学生集合并按年龄从小到大排序,年龄相同时按字母序
TreeSet<Student_hash> t = new TreeSet<>(new Comparator<Student_hash>() {
public int compare(Student_hash o1, Student_hash o2) {
int num = o1.getAge() - o2.getAge();
int num2 = num == 0 ? o1.getName().compareTo(o2.getName()) : num;// String中自带compareTo以实现字母序
return num2;
}
});// 类似快排的cmp,但实际底层数据结构是hash加链表
// 这里可以用Comparator构造排序规则,也可以在Student_hash类中连接Comparable接口并重写规则
Student_hash s1 = new Student_hash("charles", 19);
Student_hash s2 = new Student_hash("Lily", 43);
Student_hash s3 = new Student_hash("angel", 17);
Student_hash s4 = new Student_hash("Lily", 43);
t.add(s1);
t.add(s2);
t.add(s3);
t.add(s4);
for (Student_hash i : t) {
System.out.println(i.getName() + " " + i.getAge());
}
}
}
1 | package note_file.Basis.Set_Class; |
1 | package note_file.Basis.Set_Class; |
6.Thread_Class
BOX
BOX
Box_demo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package note_file.Basis.Thread_Class.Box;
public class Box_demo {
public static void main(String[] args) {
Box b = new Box();
Producer p = new Producer(b);
Customer c = new Customer(b);
Thread t1 = new Thread(p);
Thread t2 = new Thread(c);
t1.start();
t2.start();
}
}
Box.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package note_file.Basis.Thread_Class.Box;
public class Box {
private int milk;
private boolean state = false;
public synchronized void put(int milk) {
if (state) {
try {
wait();// 使该线程处于等待中
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.milk = milk;
System.out.println("送奶工将第" + this.milk + "瓶奶放入奶箱");
state = true;
notifyAll();// 唤醒其他等待的线程
}
public synchronized void get() {
if (!state) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("用户拿到第" + this.milk + "瓶奶");
state = false;
notifyAll();
}
}
Customer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package note_file.Basis.Thread_Class.Box;
public class Customer implements Runnable {
private Box b;
public Customer(Box b) {
this.b = b;
}
public void run() {
while (true) {
b.get();
}
}
}
Producer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package note_file.Basis.Thread_Class.Box;
public class Producer implements Runnable {
private Box b;
public Producer(Box b) {
this.b = b;
}
public void run() {
for (int i = 1; i <= 5; i++) {
b.put(i);
}
}
}
MyRunnable_demo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package note_file.Basis.Thread_Class;
public class MyRunnable_demo {
public static void main(String[] args) {
MyRunnable my = new MyRunnable();
Thread t1 = new Thread(my, "高铁");
Thread t2 = new Thread(my, "飞机");
t1.start();
t2.start();
}
/*
* 用runnable接口实现多线程的好处:
* 1.避免了Java单继承的局限性
* 2.适合多个相同程序的代码去处理同一个资源的情况,把线程和程序的代码、数据有效分离,较好的体现了面向对象的设计理念
*/
}
MyRunnable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package note_file.Basis.Thread_Class;
public class MyRunnable implements Runnable {
private Object obj = new Object();// 定义这个变量保证是同一把锁
public void run() {
synchronized (obj) {// synchronized用于同步代码块,相当于一把锁,使其可以让在执行期间只能有一个线程
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
}
MyThread.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package note_file.Basis.Thread_Class;
public class MyThread extends Thread {
public MyThread() {
}
public MyThread(String name) {
super(name);
}
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(getName() + ":" + i);
}
}
}
Sell_ticket_demo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package note_file.Basis.Thread_Class;
public class Sell_ticket_demo {
public static void main(String[] args) {
Sell_ticket st = new Sell_ticket();
Thread t1 = new Thread(st, "窗口1");
Thread t2 = new Thread(st, "窗口2");
Thread t3 = new Thread(st, "窗口3");
t1.start();
t2.start();
t3.start();
}
}
Sell_ticket.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package note_file.Basis.Thread_Class;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Sell_ticket implements Runnable {
private int tickets = 100;
private Lock lock = new ReentrantLock();// 设置锁
/*
* (non-Javadoc)
*
* @see java.lang.Runnable#run()
*/
public void run() {
while (true) {
try {
lock.lock();
if (tickets > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "正在出售第" + (101 - tickets) + "张票");
tickets--;
}
} finally {
lock.unlock();
}
}
}
}
Thread_demo1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package note_file.Basis.Thread_Class;
public class Thread_demo1 {
public static void main(String[] args) {
MyThread my1 = new MyThread();
MyThread my2 = new MyThread();
my1.run();
my2.run();
// 以上两行代码未启动线程
// 设置名称
// 方法1
my1.setName("charles");
my2.setName("Lily");
my1.start();
my2.start();
// 方法2:此方法需要重写方法
MyThread my3 = new MyThread("charles");
MyThread my4 = new MyThread("Lily");
my3.start();
my4.start();
// 方法3
System.out.println(Thread.currentThread().getName());// 返回当前执行线程的名字
// void start()导致此线程开始执行,Java虚拟机调用此线程的run方法
// 由运行结果可知运行为多线程运行
/*
* 线程调度
* 1.线程调度分为分时调度和抢占式调度,而java为抢占式调度
*/
MyThread by = new MyThread("公交车");
MyThread ty = new MyThread("火车");
System.out.println(by.getPriority());
System.out.println(ty.getPriority());// 线程的默认优先级为5
ty.setPriority(4);// 优先级为[1,10]
// 注:优先级高仅仅是排名靠前的几率高
by.start();
ty.start();
/*
* 多线程的实现方式有两种:
* 1.继承Thread类
* 2.实现runnable接口
*/
}
}
Thread_demo2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package note_file.Basis.Thread_Class;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;
public class Thread_demo2 {
public static void main(String[] args) {
StringBuffer bf = new StringBuffer();
StringBuilder bd = new StringBuilder();
Vector<String> v = new Vector<>();
ArrayList<String> array = new ArrayList<>();
Hashtable<String, String> ht = new Hashtable<>();
HashMap<String, String> hm = new HashMap<>();
// 以上三组前者都有synchronized关键字,为线程安全的类,后面为该类的等效替代
List<String> list = Collections.synchronizedList(new ArrayList<String>());// 返回指定列表支持的同步(线程安全)的列表
}
}
Thread_Sleep_demo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package note_file.Basis.Thread_Class;
public class Thread_Sleep_demo {
public static void main(String[] args) {
Thread_Sleep ts1 = new Thread_Sleep();
Thread_Sleep ts2 = new Thread_Sleep();
Thread_Sleep ts3 = new Thread_Sleep();
ts1.setName("关羽");
ts2.setName("刘备");// 将ts2设为主线程
ts3.setName("张飞");// 将ts3设置为守护线程
ts3.setDaemon(true);
ts1.start();
try {
ts1.join();
} catch (InterruptedException e) {
e.printStackTrace();
} // 等待该线程死亡后继续操作
ts2.start();
ts3.start();
}
}
Thread_Sleep.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package note_file.Basis.Thread_Class;
public class Thread_Sleep extends Thread {
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(getName() + ":" + i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
} // 停0.1秒
}
}
}
1 | package note_file.Basis.Thread_Class.Box; |
1 | package note_file.Basis.Thread_Class.Box; |
Customer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package note_file.Basis.Thread_Class.Box;
public class Customer implements Runnable {
private Box b;
public Customer(Box b) {
this.b = b;
}
public void run() {
while (true) {
b.get();
}
}
}
Producer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package note_file.Basis.Thread_Class.Box;
public class Producer implements Runnable {
private Box b;
public Producer(Box b) {
this.b = b;
}
public void run() {
for (int i = 1; i <= 5; i++) {
b.put(i);
}
}
}
MyRunnable_demo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package note_file.Basis.Thread_Class;
public class MyRunnable_demo {
public static void main(String[] args) {
MyRunnable my = new MyRunnable();
Thread t1 = new Thread(my, "高铁");
Thread t2 = new Thread(my, "飞机");
t1.start();
t2.start();
}
/*
* 用runnable接口实现多线程的好处:
* 1.避免了Java单继承的局限性
* 2.适合多个相同程序的代码去处理同一个资源的情况,把线程和程序的代码、数据有效分离,较好的体现了面向对象的设计理念
*/
}
MyRunnable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package note_file.Basis.Thread_Class;
public class MyRunnable implements Runnable {
private Object obj = new Object();// 定义这个变量保证是同一把锁
public void run() {
synchronized (obj) {// synchronized用于同步代码块,相当于一把锁,使其可以让在执行期间只能有一个线程
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
}
MyThread.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package note_file.Basis.Thread_Class;
public class MyThread extends Thread {
public MyThread() {
}
public MyThread(String name) {
super(name);
}
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(getName() + ":" + i);
}
}
}
Sell_ticket_demo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package note_file.Basis.Thread_Class;
public class Sell_ticket_demo {
public static void main(String[] args) {
Sell_ticket st = new Sell_ticket();
Thread t1 = new Thread(st, "窗口1");
Thread t2 = new Thread(st, "窗口2");
Thread t3 = new Thread(st, "窗口3");
t1.start();
t2.start();
t3.start();
}
}
Sell_ticket.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package note_file.Basis.Thread_Class;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Sell_ticket implements Runnable {
private int tickets = 100;
private Lock lock = new ReentrantLock();// 设置锁
/*
* (non-Javadoc)
*
* @see java.lang.Runnable#run()
*/
public void run() {
while (true) {
try {
lock.lock();
if (tickets > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "正在出售第" + (101 - tickets) + "张票");
tickets--;
}
} finally {
lock.unlock();
}
}
}
}
Thread_demo1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package note_file.Basis.Thread_Class;
public class Thread_demo1 {
public static void main(String[] args) {
MyThread my1 = new MyThread();
MyThread my2 = new MyThread();
my1.run();
my2.run();
// 以上两行代码未启动线程
// 设置名称
// 方法1
my1.setName("charles");
my2.setName("Lily");
my1.start();
my2.start();
// 方法2:此方法需要重写方法
MyThread my3 = new MyThread("charles");
MyThread my4 = new MyThread("Lily");
my3.start();
my4.start();
// 方法3
System.out.println(Thread.currentThread().getName());// 返回当前执行线程的名字
// void start()导致此线程开始执行,Java虚拟机调用此线程的run方法
// 由运行结果可知运行为多线程运行
/*
* 线程调度
* 1.线程调度分为分时调度和抢占式调度,而java为抢占式调度
*/
MyThread by = new MyThread("公交车");
MyThread ty = new MyThread("火车");
System.out.println(by.getPriority());
System.out.println(ty.getPriority());// 线程的默认优先级为5
ty.setPriority(4);// 优先级为[1,10]
// 注:优先级高仅仅是排名靠前的几率高
by.start();
ty.start();
/*
* 多线程的实现方式有两种:
* 1.继承Thread类
* 2.实现runnable接口
*/
}
}
Thread_demo2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package note_file.Basis.Thread_Class;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;
public class Thread_demo2 {
public static void main(String[] args) {
StringBuffer bf = new StringBuffer();
StringBuilder bd = new StringBuilder();
Vector<String> v = new Vector<>();
ArrayList<String> array = new ArrayList<>();
Hashtable<String, String> ht = new Hashtable<>();
HashMap<String, String> hm = new HashMap<>();
// 以上三组前者都有synchronized关键字,为线程安全的类,后面为该类的等效替代
List<String> list = Collections.synchronizedList(new ArrayList<String>());// 返回指定列表支持的同步(线程安全)的列表
}
}
Thread_Sleep_demo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package note_file.Basis.Thread_Class;
public class Thread_Sleep_demo {
public static void main(String[] args) {
Thread_Sleep ts1 = new Thread_Sleep();
Thread_Sleep ts2 = new Thread_Sleep();
Thread_Sleep ts3 = new Thread_Sleep();
ts1.setName("关羽");
ts2.setName("刘备");// 将ts2设为主线程
ts3.setName("张飞");// 将ts3设置为守护线程
ts3.setDaemon(true);
ts1.start();
try {
ts1.join();
} catch (InterruptedException e) {
e.printStackTrace();
} // 等待该线程死亡后继续操作
ts2.start();
ts3.start();
}
}
Thread_Sleep.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package note_file.Basis.Thread_Class;
public class Thread_Sleep extends Thread {
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(getName() + ":" + i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
} // 停0.1秒
}
}
}
1 | package note_file.Basis.Thread_Class.Box; |
1 | package note_file.Basis.Thread_Class.Box; |
MyRunnable_demo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package note_file.Basis.Thread_Class;
public class MyRunnable_demo {
public static void main(String[] args) {
MyRunnable my = new MyRunnable();
Thread t1 = new Thread(my, "高铁");
Thread t2 = new Thread(my, "飞机");
t1.start();
t2.start();
}
/*
* 用runnable接口实现多线程的好处:
* 1.避免了Java单继承的局限性
* 2.适合多个相同程序的代码去处理同一个资源的情况,把线程和程序的代码、数据有效分离,较好的体现了面向对象的设计理念
*/
}
MyRunnable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package note_file.Basis.Thread_Class;
public class MyRunnable implements Runnable {
private Object obj = new Object();// 定义这个变量保证是同一把锁
public void run() {
synchronized (obj) {// synchronized用于同步代码块,相当于一把锁,使其可以让在执行期间只能有一个线程
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
}
MyThread.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package note_file.Basis.Thread_Class;
public class MyThread extends Thread {
public MyThread() {
}
public MyThread(String name) {
super(name);
}
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(getName() + ":" + i);
}
}
}
Sell_ticket_demo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package note_file.Basis.Thread_Class;
public class Sell_ticket_demo {
public static void main(String[] args) {
Sell_ticket st = new Sell_ticket();
Thread t1 = new Thread(st, "窗口1");
Thread t2 = new Thread(st, "窗口2");
Thread t3 = new Thread(st, "窗口3");
t1.start();
t2.start();
t3.start();
}
}
Sell_ticket.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package note_file.Basis.Thread_Class;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Sell_ticket implements Runnable {
private int tickets = 100;
private Lock lock = new ReentrantLock();// 设置锁
/*
* (non-Javadoc)
*
* @see java.lang.Runnable#run()
*/
public void run() {
while (true) {
try {
lock.lock();
if (tickets > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "正在出售第" + (101 - tickets) + "张票");
tickets--;
}
} finally {
lock.unlock();
}
}
}
}
Thread_demo1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package note_file.Basis.Thread_Class;
public class Thread_demo1 {
public static void main(String[] args) {
MyThread my1 = new MyThread();
MyThread my2 = new MyThread();
my1.run();
my2.run();
// 以上两行代码未启动线程
// 设置名称
// 方法1
my1.setName("charles");
my2.setName("Lily");
my1.start();
my2.start();
// 方法2:此方法需要重写方法
MyThread my3 = new MyThread("charles");
MyThread my4 = new MyThread("Lily");
my3.start();
my4.start();
// 方法3
System.out.println(Thread.currentThread().getName());// 返回当前执行线程的名字
// void start()导致此线程开始执行,Java虚拟机调用此线程的run方法
// 由运行结果可知运行为多线程运行
/*
* 线程调度
* 1.线程调度分为分时调度和抢占式调度,而java为抢占式调度
*/
MyThread by = new MyThread("公交车");
MyThread ty = new MyThread("火车");
System.out.println(by.getPriority());
System.out.println(ty.getPriority());// 线程的默认优先级为5
ty.setPriority(4);// 优先级为[1,10]
// 注:优先级高仅仅是排名靠前的几率高
by.start();
ty.start();
/*
* 多线程的实现方式有两种:
* 1.继承Thread类
* 2.实现runnable接口
*/
}
}
Thread_demo2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package note_file.Basis.Thread_Class;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;
public class Thread_demo2 {
public static void main(String[] args) {
StringBuffer bf = new StringBuffer();
StringBuilder bd = new StringBuilder();
Vector<String> v = new Vector<>();
ArrayList<String> array = new ArrayList<>();
Hashtable<String, String> ht = new Hashtable<>();
HashMap<String, String> hm = new HashMap<>();
// 以上三组前者都有synchronized关键字,为线程安全的类,后面为该类的等效替代
List<String> list = Collections.synchronizedList(new ArrayList<String>());// 返回指定列表支持的同步(线程安全)的列表
}
}
Thread_Sleep_demo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package note_file.Basis.Thread_Class;
public class Thread_Sleep_demo {
public static void main(String[] args) {
Thread_Sleep ts1 = new Thread_Sleep();
Thread_Sleep ts2 = new Thread_Sleep();
Thread_Sleep ts3 = new Thread_Sleep();
ts1.setName("关羽");
ts2.setName("刘备");// 将ts2设为主线程
ts3.setName("张飞");// 将ts3设置为守护线程
ts3.setDaemon(true);
ts1.start();
try {
ts1.join();
} catch (InterruptedException e) {
e.printStackTrace();
} // 等待该线程死亡后继续操作
ts2.start();
ts3.start();
}
}
Thread_Sleep.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package note_file.Basis.Thread_Class;
public class Thread_Sleep extends Thread {
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(getName() + ":" + i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
} // 停0.1秒
}
}
}
1 | package note_file.Basis.Thread_Class; |
1 | package note_file.Basis.Thread_Class; |
MyThread.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package note_file.Basis.Thread_Class;
public class MyThread extends Thread {
public MyThread() {
}
public MyThread(String name) {
super(name);
}
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(getName() + ":" + i);
}
}
}
Sell_ticket_demo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package note_file.Basis.Thread_Class;
public class Sell_ticket_demo {
public static void main(String[] args) {
Sell_ticket st = new Sell_ticket();
Thread t1 = new Thread(st, "窗口1");
Thread t2 = new Thread(st, "窗口2");
Thread t3 = new Thread(st, "窗口3");
t1.start();
t2.start();
t3.start();
}
}
Sell_ticket.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package note_file.Basis.Thread_Class;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Sell_ticket implements Runnable {
private int tickets = 100;
private Lock lock = new ReentrantLock();// 设置锁
/*
* (non-Javadoc)
*
* @see java.lang.Runnable#run()
*/
public void run() {
while (true) {
try {
lock.lock();
if (tickets > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "正在出售第" + (101 - tickets) + "张票");
tickets--;
}
} finally {
lock.unlock();
}
}
}
}
Thread_demo1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package note_file.Basis.Thread_Class;
public class Thread_demo1 {
public static void main(String[] args) {
MyThread my1 = new MyThread();
MyThread my2 = new MyThread();
my1.run();
my2.run();
// 以上两行代码未启动线程
// 设置名称
// 方法1
my1.setName("charles");
my2.setName("Lily");
my1.start();
my2.start();
// 方法2:此方法需要重写方法
MyThread my3 = new MyThread("charles");
MyThread my4 = new MyThread("Lily");
my3.start();
my4.start();
// 方法3
System.out.println(Thread.currentThread().getName());// 返回当前执行线程的名字
// void start()导致此线程开始执行,Java虚拟机调用此线程的run方法
// 由运行结果可知运行为多线程运行
/*
* 线程调度
* 1.线程调度分为分时调度和抢占式调度,而java为抢占式调度
*/
MyThread by = new MyThread("公交车");
MyThread ty = new MyThread("火车");
System.out.println(by.getPriority());
System.out.println(ty.getPriority());// 线程的默认优先级为5
ty.setPriority(4);// 优先级为[1,10]
// 注:优先级高仅仅是排名靠前的几率高
by.start();
ty.start();
/*
* 多线程的实现方式有两种:
* 1.继承Thread类
* 2.实现runnable接口
*/
}
}
Thread_demo2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package note_file.Basis.Thread_Class;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;
public class Thread_demo2 {
public static void main(String[] args) {
StringBuffer bf = new StringBuffer();
StringBuilder bd = new StringBuilder();
Vector<String> v = new Vector<>();
ArrayList<String> array = new ArrayList<>();
Hashtable<String, String> ht = new Hashtable<>();
HashMap<String, String> hm = new HashMap<>();
// 以上三组前者都有synchronized关键字,为线程安全的类,后面为该类的等效替代
List<String> list = Collections.synchronizedList(new ArrayList<String>());// 返回指定列表支持的同步(线程安全)的列表
}
}
Thread_Sleep_demo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package note_file.Basis.Thread_Class;
public class Thread_Sleep_demo {
public static void main(String[] args) {
Thread_Sleep ts1 = new Thread_Sleep();
Thread_Sleep ts2 = new Thread_Sleep();
Thread_Sleep ts3 = new Thread_Sleep();
ts1.setName("关羽");
ts2.setName("刘备");// 将ts2设为主线程
ts3.setName("张飞");// 将ts3设置为守护线程
ts3.setDaemon(true);
ts1.start();
try {
ts1.join();
} catch (InterruptedException e) {
e.printStackTrace();
} // 等待该线程死亡后继续操作
ts2.start();
ts3.start();
}
}
Thread_Sleep.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package note_file.Basis.Thread_Class;
public class Thread_Sleep extends Thread {
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(getName() + ":" + i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
} // 停0.1秒
}
}
}
1 | package note_file.Basis.Thread_Class; |
1 | package note_file.Basis.Thread_Class; |
Sell_ticket.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package note_file.Basis.Thread_Class;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Sell_ticket implements Runnable {
private int tickets = 100;
private Lock lock = new ReentrantLock();// 设置锁
/*
* (non-Javadoc)
*
* @see java.lang.Runnable#run()
*/
public void run() {
while (true) {
try {
lock.lock();
if (tickets > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "正在出售第" + (101 - tickets) + "张票");
tickets--;
}
} finally {
lock.unlock();
}
}
}
}
Thread_demo1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package note_file.Basis.Thread_Class;
public class Thread_demo1 {
public static void main(String[] args) {
MyThread my1 = new MyThread();
MyThread my2 = new MyThread();
my1.run();
my2.run();
// 以上两行代码未启动线程
// 设置名称
// 方法1
my1.setName("charles");
my2.setName("Lily");
my1.start();
my2.start();
// 方法2:此方法需要重写方法
MyThread my3 = new MyThread("charles");
MyThread my4 = new MyThread("Lily");
my3.start();
my4.start();
// 方法3
System.out.println(Thread.currentThread().getName());// 返回当前执行线程的名字
// void start()导致此线程开始执行,Java虚拟机调用此线程的run方法
// 由运行结果可知运行为多线程运行
/*
* 线程调度
* 1.线程调度分为分时调度和抢占式调度,而java为抢占式调度
*/
MyThread by = new MyThread("公交车");
MyThread ty = new MyThread("火车");
System.out.println(by.getPriority());
System.out.println(ty.getPriority());// 线程的默认优先级为5
ty.setPriority(4);// 优先级为[1,10]
// 注:优先级高仅仅是排名靠前的几率高
by.start();
ty.start();
/*
* 多线程的实现方式有两种:
* 1.继承Thread类
* 2.实现runnable接口
*/
}
}
Thread_demo2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package note_file.Basis.Thread_Class;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;
public class Thread_demo2 {
public static void main(String[] args) {
StringBuffer bf = new StringBuffer();
StringBuilder bd = new StringBuilder();
Vector<String> v = new Vector<>();
ArrayList<String> array = new ArrayList<>();
Hashtable<String, String> ht = new Hashtable<>();
HashMap<String, String> hm = new HashMap<>();
// 以上三组前者都有synchronized关键字,为线程安全的类,后面为该类的等效替代
List<String> list = Collections.synchronizedList(new ArrayList<String>());// 返回指定列表支持的同步(线程安全)的列表
}
}
Thread_Sleep_demo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package note_file.Basis.Thread_Class;
public class Thread_Sleep_demo {
public static void main(String[] args) {
Thread_Sleep ts1 = new Thread_Sleep();
Thread_Sleep ts2 = new Thread_Sleep();
Thread_Sleep ts3 = new Thread_Sleep();
ts1.setName("关羽");
ts2.setName("刘备");// 将ts2设为主线程
ts3.setName("张飞");// 将ts3设置为守护线程
ts3.setDaemon(true);
ts1.start();
try {
ts1.join();
} catch (InterruptedException e) {
e.printStackTrace();
} // 等待该线程死亡后继续操作
ts2.start();
ts3.start();
}
}
Thread_Sleep.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package note_file.Basis.Thread_Class;
public class Thread_Sleep extends Thread {
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(getName() + ":" + i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
} // 停0.1秒
}
}
}
1 | package note_file.Basis.Thread_Class; |
1 | package note_file.Basis.Thread_Class; |
Thread_demo2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package note_file.Basis.Thread_Class;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;
public class Thread_demo2 {
public static void main(String[] args) {
StringBuffer bf = new StringBuffer();
StringBuilder bd = new StringBuilder();
Vector<String> v = new Vector<>();
ArrayList<String> array = new ArrayList<>();
Hashtable<String, String> ht = new Hashtable<>();
HashMap<String, String> hm = new HashMap<>();
// 以上三组前者都有synchronized关键字,为线程安全的类,后面为该类的等效替代
List<String> list = Collections.synchronizedList(new ArrayList<String>());// 返回指定列表支持的同步(线程安全)的列表
}
}
Thread_Sleep_demo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package note_file.Basis.Thread_Class;
public class Thread_Sleep_demo {
public static void main(String[] args) {
Thread_Sleep ts1 = new Thread_Sleep();
Thread_Sleep ts2 = new Thread_Sleep();
Thread_Sleep ts3 = new Thread_Sleep();
ts1.setName("关羽");
ts2.setName("刘备");// 将ts2设为主线程
ts3.setName("张飞");// 将ts3设置为守护线程
ts3.setDaemon(true);
ts1.start();
try {
ts1.join();
} catch (InterruptedException e) {
e.printStackTrace();
} // 等待该线程死亡后继续操作
ts2.start();
ts3.start();
}
}
Thread_Sleep.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package note_file.Basis.Thread_Class;
public class Thread_Sleep extends Thread {
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(getName() + ":" + i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
} // 停0.1秒
}
}
}
1 | package note_file.Basis.Thread_Class; |
1 | package note_file.Basis.Thread_Class; |
Thread_Sleep.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package note_file.Basis.Thread_Class;
public class Thread_Sleep extends Thread {
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(getName() + ":" + i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
} // 停0.1秒
}
}
}
1 | package note_file.Basis.Thread_Class; |
7.Other
Array_demo.java
1 | package note_file.Basis.other; |
Arrays_Class
1 | package note_file.Basis.other; |
Constant_value
1 | package note_file.Basis.other; |
Control_Statement
1 | package note_file.Basis.other; |
Input
1 | package note_file.Basis.other; |
Integer_Class
1 | package note_file.Basis.other; |
Math_Class
1 | package note_file.Basis.other; |
Method
1 | package note_file.Basis.other; |
String_Class
1 | package note_file.Basis.other; |
StringBuilder_Class
1 | package note_file.Basis.other; |
System_Class
1 | package note_file.Basis.other; |
- Title: Java_note_1
- Author: Charles
- Created at : 2022-12-29 11:32:01
- Updated at : 2026-05-11 20:11:16
- Link: https://charles2530.github.io/2022/12/29/java-note-1/
- License: This work is licensed under CC BY-NC-SA 4.0.
Comments