Skip to content

Commit d710a8f

Browse files
committed
Java第二十四天
1 parent 1e09c58 commit d710a8f

File tree

95 files changed

+1341
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+1341
-0
lines changed

day24/code/day24_Pattern/.classpath

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

day24/code/day24_Pattern/.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>day24_Pattern</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.7
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.7
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package cn.itcast_01;
2+
3+
/*
4+
* 设计模式:经验的总结。是一套模型。
5+
*
6+
* 分类:
7+
* 创建型模式
8+
* 行为型模式
9+
* 结构型模式
10+
*
11+
* 单例模式:保证类在内存中只有一个对象。
12+
* 举例:
13+
* windows打印机,网站计数器
14+
*
15+
* 如何能够保证类在内存中只有一个对象呢?
16+
* A:让外界不能创建对象
17+
* 构造私有
18+
* B:本身提供一个对象
19+
* 在成员位置创建对象
20+
* C:对外提供公共的访问方式
21+
* 写一个公共方法即可
22+
*/
23+
public class SingletDemo {
24+
public static void main(String[] args) {
25+
// Student s1 = new Student();
26+
// Student s2 = new Student();
27+
// System.out.println(s1 == s2);
28+
29+
// Student.s = null;
30+
31+
Student s1 = Student.getStudent();
32+
Student s2 = Student.getStudent();
33+
System.out.println(s1 == s2);
34+
}
35+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cn.itcast_01;
2+
3+
public class Student {
4+
// 构造私有
5+
private Student() {
6+
}
7+
8+
// 本身创建
9+
// 静态只能访问静态,加static
10+
// 为了不让外加访问,加private
11+
private static Student s = new Student();
12+
13+
// 提供公共访问
14+
// 为了让外界可以直接访问,加static
15+
public static Student getStudent() {
16+
return s;
17+
}
18+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package cn.itcast_02;
2+
3+
public class SingletDemo {
4+
public static void main(String[] args) {
5+
Teacher t1 = Teacher.getTeacher();
6+
Teacher t2 = Teacher.getTeacher();
7+
8+
System.out.println(t1 == t2);
9+
}
10+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package cn.itcast_02;
2+
3+
/*
4+
* 饿汉式:加载的时候就创建对象
5+
* 懒汉式:在用的时候再去创建对象
6+
* 线程安全问题
7+
* 延迟加载思想(什么时候用,什么时候加载)
8+
*
9+
* 面试题:请写一个Singleton
10+
* 面试:懒汉式
11+
* 开发:饿汉式
12+
*
13+
* 为什么呢?
14+
* 懒汉式容易出问题,而饿汉式不会出问题。
15+
* 线程安全问题。
16+
*/
17+
public class Teacher {
18+
private Teacher() {
19+
}
20+
21+
private static Teacher t = null;
22+
23+
public synchronized static Teacher getTeacher() {
24+
// t1,t2,t3
25+
if (t == null) {
26+
// t1,t2,t3
27+
t = new Teacher();
28+
}
29+
return t;
30+
}
31+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cn.itcast_03;
2+
3+
import java.io.IOException;
4+
5+
/*
6+
* Runtime:
7+
* public static Runtime getRuntime()
8+
*
9+
* 没有构造,方法不全是静态的。
10+
* 这中类肯定是通过静态的方法返回了该类的对象。
11+
*/
12+
public class RuntimeDemo {
13+
public static void main(String[] args) throws IOException {
14+
Runtime r = Runtime.getRuntime();
15+
16+
// r.exec("calc");
17+
r.exec("notepad");
18+
}
19+
}

day24/code/day24_Thread/.classpath

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

day24/code/day24_Thread/.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>day24_Thread</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
eclipse.preferences.version=1
2+
encoding//src/cn/itcast_07/ThreadGroupRunnableDemo.java=UTF-8
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.7
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.7
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package cn.itcast_01;
2+
3+
import java.util.concurrent.locks.Lock;
4+
import java.util.concurrent.locks.ReentrantLock;
5+
6+
public class MovieTicket implements Runnable {
7+
8+
private int tickets = 100;
9+
private Object obj = new Object();
10+
11+
// 造锁
12+
// private ReentrantLock lock = new ReentrantLock();
13+
private Lock lock = new ReentrantLock();
14+
15+
// @Override
16+
// public void run() {
17+
// while (true) {
18+
// synchronized (obj) {
19+
// if (tickets > 0) {
20+
// try {
21+
// Thread.sleep(100);
22+
// } catch (InterruptedException e) {
23+
// e.printStackTrace();
24+
// }
25+
//
26+
// System.out.println(Thread.currentThread().getName()
27+
// + "正在出售第" + (tickets--) + "张票");
28+
// }
29+
// }
30+
// }
31+
// }
32+
33+
@Override
34+
public void run() {
35+
while (true) {
36+
try {
37+
// 加锁
38+
lock.lock();
39+
40+
if (tickets > 0) {
41+
try {
42+
Thread.sleep(100);
43+
} catch (InterruptedException e) {
44+
e.printStackTrace();
45+
}
46+
47+
System.out.println(Thread.currentThread().getName()
48+
+ "正在出售第" + (tickets--) + "张票");
49+
}
50+
} finally {
51+
// 释放锁
52+
lock.unlock();
53+
}
54+
}
55+
}
56+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package cn.itcast_01;
2+
3+
/*
4+
* 为了更清楚的让我们知道是如何加锁和释放锁的,JDK5以后就提供了一个接口:Lock锁。
5+
* Lock
6+
* public void lock():加锁
7+
* public void unlock():释放锁
8+
* 实现类对象
9+
* public ReentrantLock()
10+
*/
11+
public class MovieTicketDemo {
12+
public static void main(String[] args) {
13+
MovieTicket mt = new MovieTicket();
14+
15+
Thread t1 = new Thread(mt, "窗口1");
16+
Thread t2 = new Thread(mt, "窗口2");
17+
Thread t3 = new Thread(mt, "窗口3");
18+
19+
t1.start();
20+
t2.start();
21+
t3.start();
22+
}
23+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package cn.itcast_02;
2+
3+
public class DieLock extends Thread {
4+
private boolean flag;
5+
6+
public DieLock(boolean flag) {
7+
this.flag = flag;
8+
}
9+
10+
@Override
11+
public void run() {
12+
if (flag) {
13+
synchronized (MyLock.objA) {
14+
System.out.println("if objA"); // CPUµÄÖ´ÐÐȨûÓÐÁË
15+
synchronized (MyLock.objB) {
16+
System.out.println("if objB");
17+
}
18+
}
19+
} else {
20+
synchronized (MyLock.objB) {
21+
System.out.println("else objB");
22+
synchronized (MyLock.objA) {
23+
System.out.println("else objA");
24+
}
25+
}
26+
}
27+
}
28+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cn.itcast_02;
2+
3+
/*
4+
* 死锁:是指两个或者两个以上的线程在执行的过程中,因争夺资源产生的一种互相等待现象
5+
*
6+
* 举例:
7+
* 中国人和一个美国人吃饭。
8+
* 中国人:两只筷子
9+
* 美国人:刀叉
10+
* 真实情况:
11+
* 中国人:一只筷子,一把刀
12+
* 美国人:一只筷子,一把叉
13+
*/
14+
public class DieLockDemo {
15+
public static void main(String[] args) {
16+
DieLock d1 = new DieLock(true);
17+
DieLock d2 = new DieLock(false);
18+
19+
d1.start();
20+
d2.start();
21+
}
22+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package cn.itcast_02;
2+
3+
public class MyLock {
4+
public static final Object objA = new Object();
5+
public static final Object objB = new Object();
6+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cn.itcast_03;
2+
3+
public class GetThread implements Runnable {
4+
5+
private Student s;
6+
7+
public GetThread(Student s) {
8+
this.s = s;
9+
}
10+
11+
@Override
12+
public void run() {
13+
// Student s = new Student();
14+
System.out.println(s.name + "---" + s.age);
15+
}
16+
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cn.itcast_03;
2+
3+
public class SetThread implements Runnable {
4+
5+
private Student s;
6+
7+
public SetThread(Student s) {
8+
this.s = s;
9+
}
10+
11+
@Override
12+
public void run() {
13+
// Student s = new Student();
14+
s.name = "ÁÖÇàϼ";
15+
s.age = 28;
16+
}
17+
18+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package cn.itcast_03;
2+
3+
public class Student {
4+
String name;
5+
int age;
6+
}

0 commit comments

Comments
 (0)