-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreadYield.java
71 lines (67 loc) · 1.73 KB
/
ThreadYield.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
package java_thread;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
/**
* 线程让步(yield)
*
* @author 唐龙
*/
public class ThreadYield {
//This is a test
public static void main(String []args){
Vector<String> vector=new Vector<>();
new Yield(vector).start();
new SendFile(vector).start();
}
}
/** 线程让步类 */
class Yield extends Thread {
private Vector<String> vector;
private DateFormat dateFormat = new SimpleDateFormat("HH-mm-ss:SSSS"); //日期格式
private boolean isFlag = false;
public Yield(Vector<String> vector) {
super("接收文件"); //设置线程名称
this.setDaemon(true); //如果SendFile线程结束,则该线程自动结束
this.vector=vector;
}
@Override
public void run() {
while (!isFlag) {
try {
Thread.sleep(100);//休眠0.1s
}catch(InterruptedException e){
e.printStackTrace();
}
//判断向量集合大小
if (vector.size()<=0) {
System.out.println(dateFormat.format(new Date())+ "\t集合中没有文件,执行yield操作。");
Thread.yield();//调用线程让步
} else {
String ss = vector.remove(0);
System.out.println(dateFormat.format(new Date())+"\t取到文件:" + ss);
}
}
}
}
/** 发送文件 */
class SendFile extends Thread {
private String[] files = new String[] {"新闻文件", "国内旅游向导", "山水名画欣赏", "发家致富说明"};
private Vector<String> vector;
public SendFile(Vector<String> vector) {
super("发送文件");
this.vector=vector;
}
@Override
public void run() {
try {
for (int i=0;i < files.length;i++){
Thread.sleep(200);//线程休眠0.2s
vector.add(files[i]);//添加文件
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}