一些JAVA基础知识
1、数组
1.1 创建与初始化
1 | int[] a = new int[] {1,3,8,1,55,6}; |
1.2 复制数组
1 | System.arraycopy(a, 0, b, 1, 1); // 从a数组的0下标复制1个到b数组的1下标 |
1.3 二维数组
1 | class ErWeiShuZu { |
1.4 数组作为方法参数
1 | class ArrayUse { |
主函数调用:
1 | int[] a2 = new int[] {1,3,8,1,55,6}; |
1.5 排序
1 | import java.util.Scanner; |
2、类与对象
2.1 命名规范
- 类的第一个字母大写;
- 方法addSpeed().
- 属性一般小写.
2.2 引用
引用与指向
new一个对象仅仅代表新建一个对象,没有办法访问它;而使用=给引用赋对象值,代表该对象,又叫“指向”;1
2//使用一个引用来指向这个对象
Lei h1 = new Lei();多个引用,一个对象
1
2
3
4
5
6
7
8
9
10
11
12//使用一个引用来指向这个对象
Lei h1 = new Lei();
System.out.println(h1.name);
h1.name = "Cct";
Lei h2 = h1; //h2指向h1所指向的对象
Lei h3 = h1;
Lei h4 = h1;
Lei h5 = h4;
System.out.println(h5.name);
h4.name = "abc";
System.out.println(h1.name);
//h1,h2,h3,h4,h5 五个引用,都指向同一个对象一个引用,多个对象
当某个对象没有任何引用指向,就会被垃圾回收。
2.3 this的指向
1 | class Hero { |
2.4 函数对象参数改变不影响实参
1 |
|
2.5 对象属性初始化顺序
声明函数初始化 -> 初始化块 -> 构造函数初始化
1 | class Hero3 { |
2.6 枚举类
1 | class EnumLei { |
2.7 单例模式
饿汉式单例模式
立即加载
延时加载
与上面不同的是,类属性先不指向一个对象,而是在getInstance()中判断该引用是否为null,若是再new一个,否则直接返回。
3、接口与继承
接口
1 | package animalUML; |
抽象类
1 | package animalUML; |
继承
1 | package animalUML; |
4、多态
接口
1 | package DuoTai; |
类
1 | /* |
主函数
1 | package DuoTai; |
5、文件输入输出流
5.1 一些文件函数
1 | File f = new File("src"); |
5.2 获取文件列表
1 | void getFiles(File[] fs) { |
5.2 字节流读取文件
1 | void readFile(File f) { |
5.3 字节流写入文件
1 | void writeFile(File f) { |
5.4 字符流读取文件
1 | void readFile2(File f) { |
5.5 字符流写入文件
1 | void writeFile2(File f) { |
6、集合框架
6.1 arraylist
1 | package arrayListLearn; |
6.2 linkedlist
1 | package linkListLearn; |
6.3 hashset
1 | package hashLearn; |
7、匿名类
接口方法
1 | package lambdaLearn; |
主函数
1 | package lambdaLearn; |
8、多线程
8.1 线程的一些基本操作
实现线程的3种方式:
- 继承Thread
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19package multipleThread;
import DuoTai.Hero;
public class KillThread extends Thread{
private Hero h1;
private Hero h2;
public KillThread(Hero h1, Hero h2) {
this.h1 = h1;
this.h2 = h2;
}
public void run() {
while (!h2.isDead()) {
h1.attackHero(h2);
}
}
} - Battle类实现runnable接口:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20package multipleThread;
import DuoTai.Hero;
public class Battle implements Runnable{
Hero h1;
Hero h2;
Battle(Hero h1, Hero h2) {
this.h1 = h1;
this.h2 = h2;
}
public void run() {
// TODO Auto-generated method stub
while (!h2.isDead()) {
h1.attackHero(h2);
}
}
} - 匿名类
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/*
* 多线程例子
*/
package multipleThread;
import DuoTai.Hero;
public class MultipleThread {
public static void main(String[] args) {
// TODO Auto-generated method stub
Hero h1 = new Hero("A", 2500, 5);
Hero h2 = new Hero("B", 2000, 8);
/*
* 顺序
while (!h2.isDead()) {
h1.attackHero(h2);
}
while (!h1.isDead()) {
h2.attackHero(h1);
}
*/
// 多线程
// 继承Thread
/*
KillThread kt1 = new KillThread(h1, h2);
kt1.start(); // 借助一个线程对象的start方法,才会启动一个线程
KillThread kt2 = new KillThread(h2, h1);
kt2.start();
*/
//或实现Runnable接口
/*
Battle b1 = new Battle(h1, h2);
Battle b2 = new Battle(h2, h1);
new Thread(b2).start();
new Thread(b1).start();
*/
// 或匿名类
// A攻击B
Thread t1 = new Thread() {
public void run() {
// 可直接使用外部final变量
// JDK7之后无要求
while (!h2.isDead()) {
h1.attackHero(h2);
}
}
};
// B攻击A
Thread t2 = new Thread() {
public void run() {
while (!h1.isDead()) {
Thread.yield(); // 临时暂停
h2.attackHero(h1);
}
}
};
t1.setPriority(1);
t2.setPriority(10); // 优先级大的抢占CPU能力强
t1.start();
t2.start();
/*
try {
t1.join(); // 加入到主线程中
t2.join();
} catch(InterruptedException e) {
e.printStackTrace();
}
*/
System.out.println("Game Over!!!");
}
}8.2 原子操作
普通int不能做到并发操作,而AtomicInteger可以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
54package multipleThread;
import java.util.concurrent.atomic.AtomicInteger;
public class AtomicTest {
private static int value = 0;
private static AtomicInteger aint = new AtomicInteger();
public static void main(String[] args) {
// TODO Auto-generated method stub
int number = 100000;
Thread[] t1 = new Thread[number];
for (int i = 0; i < number; i++) {
Thread t = new Thread() {
public void run() {
value++;
}
};
t.start();
t1[i] = t;
}
for (Thread t: t1) {
try {
t.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("value:" + value);
Thread t2[] = new Thread[number];
for (int i = 0; i < number; i++) {
Thread t = new Thread() {
public void run() {
aint.incrementAndGet(); // 原子操作:自增
}
};
t.start();
t2[i] = t;
}
for (Thread t:t2) {
try {
t.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("aint:" + aint.intValue());
}
}8.3 synchronized
Hero类的recover方法与hurt方法有synchronized关键词修饰: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/**
* 进程同步
*/
package multipleThread;
import DuoTai.Hero;
public class KeyTest {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
final Hero h = new Hero("cct", 1000, 50);
System.out.printf("初始血量为%d\n", h.hp);
// n个线程
int n = 10000;
Thread[] addThreads = new Thread[n];
Thread[] reduceThreads = new Thread[n];
// 匿名类建立n个线程增加hp
for (int i = 0; i < n; i++) {
Thread t = new Thread() {
public void run() {
h.recover();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
t.start();
addThreads[i] = t;
}
// 匿名类建立n个线程减少hp
for (int i = 0; i < n; i++) {
Thread t = new Thread() {
public void run() {
h.hurt();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
t.start();
reduceThreads[i] = t;
}
// 等待所有增加线程结束
for (Thread t : addThreads) {
try {
t.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 等待所有减少线程结束
for (Thread t : reduceThreads) {
t.join();
}
System.out.print(h.hp); // 初始血量1000
}
}8.4 await signal signalAll
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
89package multipleThread;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class LockTest {
// 获取当前时间
public static String now() {
return new SimpleDateFormat("HH:mm:ss").format(new Date());
}
// 打印日志
public static void log(String msg) {
System.out.printf("%s %s %s%n", now(), Thread.currentThread().getName(), msg);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Lock lock = new ReentrantLock();
Condition condition = lock.newCondition();// await signal signalAll方法
Thread t1 = new Thread() {
public void run() {
boolean locked = false;
try {
log("线程启动");
log("试图lock");
lock.lock();
log("已占有(5s)");
Thread.sleep(5000);
log("临时释放lock对象,并等待");
condition.await(); // 自身陷入等待
log("重新占有lock,继续5s");
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
log("释放对象unlock");
lock.unlock();
}
log("线程结束");
}
};
t1.setName("t1");
t1.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Thread t2 = new Thread() {
public void run() {
boolean locked = true;
try {
log("线程启动");
log("试图lock");
// lock.lock();
locked = lock.tryLock(6, TimeUnit.SECONDS); // 试图占有操作,时限1s
if (locked) {
log("已占有(5s)");
Thread.sleep(5000);
log("唤醒");
condition.signal();
} else {
log("1s内没能成功占有 Sorry");
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (locked) { // 在占有对象的前提下
log("释放对象unlock");
// lock.unlock();
}
}
log("线程结束");
}
};
t2.setName("t2");
t2.start();
}
}8.5 死锁实例
hero实例对象使用synchronized修饰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
69package multipleThread;
import DuoTai.Hero;
public class LockDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
final Hero h1 = new Hero("hero1", 1000, 10);
final Hero h2 = new Hero("hero2", 1000, 10);
final Hero h3 = new Hero("hero3", 1000, 10);
Thread t1 = new Thread() {
public void run() {
synchronized (h1) {
System.out.println("t1占有h1!");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("t1想去占有h2");
synchronized (h2) {
System.out.println("nothing");
}
}
}
};
t1.start();
Thread t2 = new Thread() {
public void run() {
synchronized (h2) {
System.out.println("t2占有h2!");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("t2想去占有h3");
synchronized (h3) {
System.out.println("nothing");
}
}
}
};
t2.start();
Thread t3 = new Thread() {
public void run() {
synchronized (h3) {
System.out.println("t3占有h3!");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("t3想去占有h1");
synchronized (h1) {
System.out.println("nothing");
}
}
}
};
t3.start();
}
}8.6 生产者与消费者
两种方式synchronized与信号量方式
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/*
* 生产者-消费者
*/
package multipleThread;
import java.util.Random;
import java.util.Stack;
class MyStack<E> {
private Stack<E> ms = new Stack<E>();
private int limit = 10; // 栈的大小
public MyStack(int l) {
this.limit = l;
}
// 在栈满的情况下 使当前进程wait
// 将item压入栈
// 通知消费者wait进程可拿
synchronized void push(E item) throws InterruptedException {
while (ms.size() == limit) this.wait(); // 使用while再次确认 防止虚假唤醒
ms.push(item);
System.out.println("当前stack大小为"+ms.size());
notifyAll();
}
// 在栈空的情况下 使当前进程wait
// 将item取出栈
// 通知生产者wait进程可放
synchronized E pop() throws InterruptedException {
while (ms.size() == 0) this.wait();
E item = ms.pop();
System.out.println("当前stack大小为"+ms.size());
notifyAll();
return item;
}
}
public class PCproblem {
public static void main(String[] args) {
MyStack<Integer> ms = new MyStack(10); // 临界区
// TODO Auto-generated method stub
Thread producer = new Thread() {
public void run() {
Random r = new Random();
try {
while (true) {
int x = r.nextInt(10);
ms.push(x);
System.out.println("压入"+ x);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
producer.start();
Thread[] consumer = new Thread[3];
for (int i = 0; i < 3; i++) {
Thread t = new Thread() {
public void run() {
try {
while (true) {
int x = ms.pop();
System.out.println("弹出"+ x);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
consumer[i] = t;
t.start();
}
}
}lock法
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117/*
* 生产者-消费者
*/
package multipleThread;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.Stack;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class MyNewStack<E> {
private Stack<E> ms = new Stack<E>();
private int limit = 10; // 栈的大小
Lock lock = new ReentrantLock(); // 创建锁对象
Condition condition = lock.newCondition();
// 获取当前时间
public static String now() {
return new SimpleDateFormat("HH:mm:ss").format(new Date());
}
// 打印日志
public static void log(String msg) {
System.out.printf("%s %s %s%n", now(), Thread.currentThread().getName(), msg);
}
public MyNewStack(int l) {
this.limit = l;
}
// 在栈满的情况下 使当前进程wait 释放锁
// 不能使用synchronized关键字,因为使用后确保对stack的互斥访问 尽管await了,还是上着锁
// 当栈不满后,将item压入栈
// 通知消费者wait进程可拿
void push(E item) throws InterruptedException {
try {
lock.lock();
while (ms.size() == limit) {
log("栈满了,生产者等待");
condition.await(); // 使用while再次确认 防止虚假唤醒
}
ms.push(item);
log("生产者压入"+ item);
condition.signal(); // 通知消费者
} finally {
lock.unlock();
}
}
// 在栈空的情况下 使当前进程wait
// 将item取出栈
// 通知生产者wait进程可放
E pop() throws InterruptedException {
try {
lock.lock();
while (ms.size() == 0) {
log("栈为空,消费者等待");
condition.await();
}
E item = ms.pop();
log("消费者弹出"+item);
condition.signal();
return item;
} finally {
lock.unlock(); // 解锁
}
}
}
public class PCproblem2 {
public static void main(String[] args) {
MyNewStack<Integer> ms = new MyNewStack(10); // 临界区
// TODO Auto-generated method stub
// 1个生产者线程
Thread producer = new Thread() {
public void run() {
ms.log("启动");
Random r = new Random();
try {
while (true) {
int x = r.nextInt(10);
ms.push(x);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
producer.setName("producer");
producer.start();
// 3个消费者线程
Thread[] consumer = new Thread[3];
for (int i = 0; i < 3; i++) {
Thread t = new Thread() {
public void run() {
ms.log("启动");
try {
while (true) {
int x = ms.pop();
ms.log("thousand years later...");
Thread.sleep(5000);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
consumer[i] = t;
t.setName("consumer" + (i + 1));
t.start();
}
}
}8.7 线程池
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121package multipleThread;
import java.util.LinkedList;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
class MyThreadPool {
int poolSize;
// 任务容器
LinkedList<Runnable> tasks = new LinkedList<Runnable>();
// 10个消费者进程
public MyThreadPool() {
poolSize = 10;
for (int i = 0; i < 10; i++) {
new ConsumeThread("线程"+i).start();
}
}
// 加入到任务容器中
public void add(Runnable r) {
synchronized (tasks) {
tasks.add(r);
// 通知wait的消费者进程
tasks.notifyAll();
}
}
// 子类
// 消费者线程
class ConsumeThread extends Thread {
public ConsumeThread(String name) {
super(name);
}
Runnable task;
public void run() {
System.out.println("启动:" + this.getName());
while (true) { // 启动后一直保持运行状态
synchronized (tasks) { // 对tasks同步访问
// 任务容器为空 wait
while (tasks.isEmpty()) {
try {
tasks.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 从任务容器中取出任务
task = tasks.removeLast();
// 通知生产任务线程继续生产
tasks.notifyAll();
}
System.out.println(this.getName()+"获取到任务,并执行");
task.run();
}
}
}
}
public class ThreadPool {
private void selfThreadPoolMethods() {
// 线程数
// 最大线程数
// 多余线程被回收的空余时间
ThreadPoolExecutor tpe = new ThreadPoolExecutor(10,15,60,TimeUnit.SECONDS,new LinkedBlockingQueue<Runnable>());
for (int i = 0; i < 20; i++) {
System.out.println(i);
tpe.execute(new Runnable() {
public void run() {
// TODO Auto-generated method stub
System.out.println("do something!");
try { // 拉长任务执行时间
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new ThreadPool().selfThreadPoolMethods();
System.out.println("end");
/*
MyThreadPool mtp = new MyThreadPool();
int slptime = 1000;
// 加入任务
for (int i = 0; i < 100; i++) {
Runnable r = new Runnable() {
public void run() {
System.out.println("do something!");
try { // 拉长任务执行时间
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
mtp.add(r);
try {
slptime = slptime > 100 ? slptime - 100 : slptime;
Thread.sleep(slptime); // 缩短任务加入间隔
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
*/
}
}