Skip to content

Commit a7c5985

Browse files
author
chenweijie
committed
添加delayQueue延迟队列的demo
1 parent dabbdad commit a7c5985

4 files changed

Lines changed: 205 additions & 8 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.chen.api.util.classloader;
2+
3+
/**
4+
* 类加载的实例
5+
* @author : chen weijie
6+
* @Date: 2018-10-19 11:34 AM
7+
*/
8+
public class ClassLoadDemo {
9+
10+
static {
11+
System.out.println("ClassLoadDemo静态初始化块执行了");
12+
}
13+
14+
public static void main(String[] args) throws ClassNotFoundException {
15+
16+
ClassLoader loader = ClassLoadDemo.class.getClassLoader();
17+
System.out.println(loader);
18+
19+
//loader.loadClass()来加载类,不会执行静态初始化模块
20+
loader.loadClass("com.chen.algorithm.bsearch.Bsearch");
21+
//class.forName加载,默认回执行静态初始化模块
22+
Class.forName("com.chen.algorithm.bsearch.Bsearch");
23+
// 使用Class.forName()来加载类,并指定ClassLoader,初始化时不执行静态块
24+
Class.forName("com.chen.algorithm.bsearch.Bsearch",false,loader);
25+
26+
27+
28+
29+
}
30+
31+
32+
33+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.chen.api.util.classloader;
2+
3+
/**
4+
* 类加载器的获取
5+
*
6+
* @author : chen weijie
7+
* @Date: 2018-10-19 11:22 AM
8+
*/
9+
public class HelloWorld {
10+
11+
12+
/**
13+
* 三、类加载的一般过程
14+
* 原理:双亲委托模式
15+
* 1、寻找jre目录,寻找jvm.dll,并初始化JVM;
16+
* 2、产生一个Bootstrap Loader(启动类加载器);
17+
* 3、Bootstrap Loader自动加载Extended Loader(标准扩展类加载器),并将其父Loader设为Bootstrap Loader。
18+
* 4、Bootstrap Loader自动加载AppClass Loader(系统类加载器),并将其父Loader设为Extended Loader。
19+
* 5、最后由AppClass Loader加载HelloWorld类。
20+
* <p>
21+
* 四、类加载器的特点
22+
* 1、运行一个程序时,总是由AppClass Loader(系统类加载器)开始加载指定的类。
23+
* 2、在加载类时,每个类加载器会将加载任务上交给其父,如果其父找不到,再由自己去加载。
24+
* 3、Bootstrap Loader(启动类加载器)是最顶级的类加载器了,其父加载器为null.
25+
*/
26+
27+
public static void main(String[] args) {
28+
29+
HelloWorld hello = new HelloWorld();
30+
Class<?> c = hello.getClass();
31+
ClassLoader classLoader = c.getClassLoader();
32+
System.out.println(classLoader);
33+
System.out.println(classLoader.getParent());
34+
// 获取不到其父类,因为其父类是用C语言写的,找不到一个确定的返回父Loader的方式
35+
System.out.println(classLoader.getParent().getParent());
36+
37+
38+
39+
}
40+
41+
42+
}

‎src/main/java/com/chen/api/util/netty/BIOTest.java‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@
77
public class BIOTest {
88

99

10-
public static void main(String[] args) {
11-
12-
13-
14-
15-
16-
17-
}
1810

1911

2012
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package com.chen.api.util.thread.delaythread;
2+
3+
import java.util.Iterator;
4+
import java.util.Random;
5+
import java.util.concurrent.DelayQueue;
6+
import java.util.concurrent.Delayed;
7+
import java.util.concurrent.TimeUnit;
8+
9+
/**
10+
* @author : chen weijie
11+
* @Date: 2018-10-20 11:04 PM
12+
*/
13+
public class DelayQueueDemoOne {
14+
15+
16+
public static void main(String[] args) {
17+
// TODO Auto-generated method stub
18+
int studentNumber = 20;
19+
DelayQueue<Student> students = new DelayQueue<>();
20+
Random random = new Random();
21+
for (int i = 0; i < studentNumber; i++) {
22+
students.put(new Student("student" + (i + 1), 30 + random.nextInt(120)));
23+
}
24+
students.put(new Student("student", 120));
25+
Thread teacherThread = new Thread(new Teacher(students));
26+
teacherThread.start();
27+
}
28+
}
29+
30+
class Student implements Runnable, Delayed {
31+
32+
private String name;
33+
public long workTime;
34+
private long submitTime;
35+
private boolean isForce = false;
36+
37+
public Student() {
38+
}
39+
40+
public Student(String name, long workTime) {
41+
this.name = name;
42+
this.workTime = workTime;
43+
// 纳秒级别
44+
this.submitTime = TimeUnit.NANOSECONDS.convert(workTime, TimeUnit.NANOSECONDS) + System.nanoTime();
45+
}
46+
47+
@Override
48+
public int compareTo(Delayed o) {
49+
// TODO Auto-generated method stub
50+
if (o == null || !(o instanceof Student)){
51+
return 1;
52+
}
53+
if (o == this){
54+
return 0;
55+
}
56+
Student s = (Student) o;
57+
if (this.workTime > s.workTime) {
58+
return 1;
59+
} else if (this.workTime == s.workTime) {
60+
return 0;
61+
} else {
62+
return -1;
63+
}
64+
}
65+
66+
@Override
67+
public long getDelay(TimeUnit unit) {
68+
// TODO Auto-generated method stub
69+
return unit.convert(submitTime - System.nanoTime(), TimeUnit.NANOSECONDS);
70+
}
71+
72+
@Override
73+
public void run() {
74+
// TODO Auto-generated method stub
75+
if (isForce) {
76+
System.out.println(name + " 交卷,实际用时 120分钟");
77+
} else {
78+
System.out.println(name + " 交卷," + "实际用时 " + workTime + " 分钟");
79+
}
80+
}
81+
82+
public boolean isForce() {
83+
return isForce;
84+
}
85+
86+
public void setForce(boolean isForce) {
87+
this.isForce = isForce;
88+
}
89+
90+
}
91+
92+
class Teacher implements Runnable {
93+
private int counter = 20;
94+
private DelayQueue<Student> students;
95+
96+
public Teacher(DelayQueue<Student> students) {
97+
this.students = students;
98+
}
99+
100+
@Override
101+
public void run() {
102+
// TODO Auto-generated method stub
103+
try {
104+
System.out.println(" test start");
105+
while (counter > 0) {
106+
Student student = students.poll();
107+
if (student.workTime < 120) {
108+
student.run();
109+
if (counter > 0) {
110+
counter--;
111+
}
112+
} else {
113+
System.out.println(" 考试时间到,全部交卷!");
114+
Student tmpStudent;
115+
for (Iterator<Student> iterator2 = students.iterator(); iterator2.hasNext(); ) {
116+
tmpStudent = iterator2.next();
117+
tmpStudent.setForce(true);
118+
tmpStudent.run();
119+
if (counter > 0) {
120+
counter--;
121+
}
122+
}
123+
}
124+
}
125+
} catch (Exception e) {
126+
// TODO: handle exception
127+
e.printStackTrace();
128+
}
129+
}
130+
}

0 commit comments

Comments
 (0)