项目场景:数据采集需要多线程执行,在所有子线程结束后主线程写入文件采集时间的结束点,但是在数据采集的时候也读取这个时间点
问题:当多个线程在读文件的时候主线程同时在写文件,导致多个线程读到的时间不一致,或者就是线程读不到时间,出现数据丢失
解决:
1.创建线程池代码如下
ExecutorService
executorService
= Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()*5);
for
(
int
i
= 0;
i
< 5;
i
++){
executorService
.execute(
new
ThreadDemo());
}
//禁止添加新的线程
executorService
.shutdown();
//等待所有线程结束后执行
if
(
executorService
.awaitTermination(Long.
MAX_VALUE
, TimeUnit.
MINUTES
)){
System.
out
.println(
"i am main thread"
);
}
2.创建线程组
ThreadGroup
g
=
new
ThreadGroup(
"g1"
);
for
(
int
i
= 0;
i
< 2;
i
++) {
Thread
t
=
new
Thread(
g
,
new
ThreadDemo());
t
.start();
}
// 获得当前线程组中线程数目, 包括可运行和不可运行的
//当前的线程数为零时表示线程组的线程执行结束
while
(
g
.activeCount() > 0){
// System.out.println("group1的当前的活跃数量:"+g.activeCount());
}
System.
out
.println(
"==g中所有线程执行结束==="
);
3.使用join(),线程会依次执行,会影响执行效率
public
class
JoinDemo {
public
static
void
main(String[]
args
)
throws
InterruptedException {
for
(
int
i
= 0 ;
i
< 3 ;
i
++){
JoinFoo
foo
=
new
JoinFoo();
foo
.start();
foo
.join();
}
System.
out
.println(
"main Thread over"
);
}
static
class
JoinFoo
extends
Thread {
int
total
;
@Override
public
void
run(){
System.
out
.println(
"thread 1 is running"
);
try
{
Thread. sleep(2000);
}
catch
(InterruptedException
e
) {
//
TODO
Auto-generated catch block
e
.printStackTrace();
}
System.
out
.println(
"thread 1 is over and set flag to 1"
);
}
}
}
public
class
ThreadDemo
extends
Thread{
public
int
i
= 0;
public
ThreadDemo(){
super
();
}
@Override
public
void
run(){
System.
out
.println(Thread.currentThread().getName()+
"====="
+(++
i
));
System.
out
.println(
"子线程"
+Thread.currentThread ().getName());
try
{
Thread. sleep(1000);
}
catch
(InterruptedException
e
) {
//
TODO
Auto-generated catch block
e
.printStackTrace();
}
}
}
对于单个线程等待的可使用标记
public
class
TestSync {
private
static
int
FLAG
= 0;
public
static
void
main(String[]
args
) {
Thread1
thread1
=
new
Thread1();
System.
out
.println(
"thread 1 is start"
);
thread1
.start();
while
(
FLAG
!= 1) {
// System.out.println("main thread is waiting");
}
FLAG
= 0;
System.
out
.println(
"main thread is back to work"
);
}
static
class
Thread1
extends
Thread {
int
total
;
public
void
run() {
System.
out
.println(
"thread 1 is running"
);
for
(
int
i
= 0;
i
< 5;
i
++) {
total
=
total
+
i
;
System.
out
.println(
"total = "
+
total
);
}
System.
out
.println(
"thread 1 is over and set flag to 1"
);
FLAG
= 1;
}
}
}