线程同步
1.题目解析2.线程同步demo2.1LockSupport Demo2.2自旋 Demo2.3阻塞队列 Demo2.4 synchronized Demo2.5阻塞队列 Demo结果
1.题目解析
循环打印出,肯定需要两个进程,如何保障两个进程有序打印呢?这就涉及到的线程同步问题。
使用同步锁的帮助类:LockSupport 两个重要方法
public static void unpark(Thread thread
);
public static void park();
使用自旋 占用cpu时间;
使用阻塞队列: BlockingDeque
put(E e
);
take();
使用synchronized,notify,wait使用lock,condition;
Lock lock
= new ReentrantLock();
Condition condition
= lock
.newCondition();
condition
.signal();
condition
.wait();
2.线程同步demo
2.1LockSupport Demo
public clsaa Demo
{
static Thread t1
= null
;
static Thread t2
= null
;
public static void main(String
[] args
) throws InterruptedException
{
List
<Integer> numberList
= Lists
.newArrayList(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);
List
<String> stringList
= Lists
.newArrayList("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");
List
<String> resultList
= Lists
.newArrayListWithExpectedSize(numberList
.size()+stringList
.size());
t1
= new Thread(() -> {
for (Integer integer
: numberList
) {
resultList
.add(String
.valueOf(integer
));
LockSupport
.unpark(t2
);
LockSupport
.park();
}
});
t2
= new Thread(() -> {
for (String str
: stringList
) {
LockSupport
.park();
resultList
.add(str
);
LockSupport
.unpark(t1
);
}
});
t1
.start();
t2
.start();
Thread
.sleep((long) (10 * 10));
System
.out
.println(resultList
);
System
.out
.println(resultList
.stream().collect(Collectors
.joining()));
}
}
2.2自旋 Demo
public clsaa Demo
{
static Thread t1
= null
;
static Thread t2
= null
;
enum ThreadToRun
{
T1
, T2
}
static volatile ThreadToRun threadToRun
= ThreadToRun
.T1
;
public static void main(String
[] args
) throws InterruptedException
{
List
<Integer> numberList
= Lists
.newArrayList(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);
List
<String> stringList
= Lists
.newArrayList("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");
List
<String> resultList
= Lists
.newArrayListWithExpectedSize(numberList
.size() + stringList
.size());
t1
= new Thread(() -> {
for (Integer integer
: numberList
) {
while (threadToRun
!= ThreadToRun
.T1
) {}
resultList
.add(String
.valueOf(integer
));
threadToRun
= ThreadToRun
.T2
;
}
});
t2
= new Thread(() -> {
for (String str
: stringList
) {
while (threadToRun
!= ThreadToRun
.T2
) {}
resultList
.add(String
.valueOf(str
));
threadToRun
= ThreadToRun
.T1
;
}
});
t1
.start();
t2
.start();
Thread
.sleep((long) (10 * 100));
System
.out
.println(resultList
);
System
.out
.println(String
.join("", resultList
));
}
2.3阻塞队列 Demo
2.4 synchronized Demo
public static void main(String
[] args
) throws InterruptedException
{
List
<Integer> numberList
= Lists
.newArrayList(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);
List
<String> stringList
= Lists
.newArrayList("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");
List
<String> resultList
= Lists
.newArrayListWithExpectedSize(numberList
.size() + stringList
.size());
Object o
= new Object();
t1
= new Thread(() -> {
synchronized (o
) {
for (Integer integer
: numberList
) {
resultList
.add(String
.valueOf(integer
));
try {
o
.notify();
o
.wait();
} catch (Exception e
) {
}
}
o
.notify();
}
});
t2
= new Thread(() -> {
synchronized (o
) {
for (String str
: stringList
) {
resultList
.add(str
);
try {
o
.notify();
o
.wait();
} catch (Exception e
) {
}
}
o
.notify();
}
});
t1
.start();
t2
.start();
Thread
.sleep((long) (10 * 100));
System
.out
.println(resultList
);
System
.out
.println(String
.join("", resultList
));
}
2.5阻塞队列 Demo
结果