This commit is contained in:
xiangwbs
2018-04-22 10:43:56 +08:00
parent 6581682add
commit e8b5f3593e
3 changed files with 80 additions and 110 deletions
@@ -8,6 +8,7 @@ import java.util.Date;
/**
* 创建日期: 2017年2月20日 上午11:32:55
* 作者: xwb
* 说明:线程api demo
*/
public class ThreadDemo {
@@ -34,31 +35,27 @@ public class ThreadDemo {
* 使用匿名内部类实现两种创建线程的方式
*/
// 方式一:
Thread thread1 = new Thread() {
Thread thread1 = new Thread() {//直接创建线程并重写run方法
public void run() {
// TUDO
}
};
// 方式二:
Runnable runnable = new Runnable() {
public void run() {
Thread.yield();////模拟cpu时间片耗尽,线程发生切换
}
Runnable runnable = () -> {//创建runable并重写run方法,创建线程并调用相应的runable
Thread.yield();//模拟cpu时间片耗尽,线程发生切换
};
Thread thread2 = new Thread(runnable);
// 方式三:匿名对象方式(推荐)
Thread thread3 = new Thread(new Runnable() {// 使用sleep阻塞,实现电子表功能
public void run() {
while (true) {
System.out.println(DateUtil2.dateToStr(new Date(),DateUtil2.YYYY_MM_DD_HH_MM_SS));
try {
Thread.sleep(1000);// 线程进入n毫秒阻塞状态
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
// 方式三:匿名对象方式
Thread thread3 = new Thread(() -> {// 使用sleep阻塞,实现电子表功能
while (true) {
System.out.println(DateUtil2.dateToStr(new Date(), DateUtil2.YYYY_MM_DD_HH_MM_SS));
try {
Thread.sleep(1000);// 线程进入n毫秒阻塞状态
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
/**
* 获取线程相关信息的api
@@ -83,14 +80,12 @@ public class ThreadDemo {
* 后台线程,又叫做守护线程 当一个进程中的所有前台线程都结束了,进程就会结束
* 无论进程中的其他后台线程是否还在运行,都要被强制中断。
*/
Thread rose = new Thread() {// 前台线程
public void run() {
}
};
Thread jack = new Thread() {// 后台线程
public void run() {
}
};
// 前台线程
Thread rose = new Thread(() -> {
});
// 后台线程
Thread jack = new Thread(() -> {
});
// 设置为后台线程,需要在start前调用
jack.setDaemon(true);
rose.start();
@@ -105,21 +100,17 @@ public class ThreadDemo {
*/
class Join {
public static void main(String[] args) {
final Thread t1 = new Thread() {
public void run() {
//一些耗时的操作
final Thread t1 = new Thread(() -> {
//一些耗时的操作
});
Thread t2 = new Thread(() -> {
try {
t1.join();//这里t2线程会开始阻塞,直到t1线程的run方法执行完毕
} catch (InterruptedException e) {
e.printStackTrace();
}
};
Thread t2 = new Thread() {
public void run() {
try {
t1.join();//这里t2线程会开始阻塞,直到t1线程的run方法执行完毕
} catch (InterruptedException e) {
e.printStackTrace();
}
//以下是当前线程的任务代码,只有t1线程运行完毕才会运行。
}
};
//以下是当前线程的任务代码,只有t1线程运行完毕才会运行。
});
t1.start();
t2.start();
}
@@ -22,7 +22,7 @@ class PublicResource {
/**
* 增加公共资源
*/
public synchronized void increace() {
synchronized void inCreace() {
while (number != 0) {
try {
wait();
@@ -38,7 +38,7 @@ class PublicResource {
/**
* 减少公共资源
*/
public synchronized void decreace() {
synchronized void deCreace() {
while (number == 0) {
try {
wait();
@@ -58,7 +58,7 @@ class PublicResource {
class ProducerThread implements Runnable {
private PublicResource resource;
public ProducerThread(PublicResource resource) {
ProducerThread(PublicResource resource) {
this.resource = resource;
}
@@ -70,7 +70,7 @@ class ProducerThread implements Runnable {
} catch (InterruptedException e) {
e.printStackTrace();
}
resource.increace();
resource.inCreace();
}
}
}
@@ -81,7 +81,7 @@ class ProducerThread implements Runnable {
class ConsumerThread implements Runnable {
private PublicResource resource;
public ConsumerThread(PublicResource resource) {
ConsumerThread(PublicResource resource) {
this.resource = resource;
}
@@ -93,7 +93,7 @@ class ConsumerThread implements Runnable {
} catch (InterruptedException e) {
e.printStackTrace();
}
resource.decreace();
resource.deCreace();
}
}
}
@@ -9,7 +9,6 @@ import java.util.Map;
import java.util.Set;
/**
* 说明:
* 多线程并发安全问题
@@ -25,17 +24,9 @@ public class ThreadSyncDemo {
/*
* 同步代码块
*/
final Shop s=new Shop();//保证对象一致
Thread t1=new Thread(){
public void run(){
s.buy();
}
};
Thread t2=new Thread(){
public void run(){
s.buy();
}
};
final Shop s = new Shop();//保证对象一致
Thread t1 = new Thread(s::buy);
Thread t2 = new Thread(s::buy);
t1.start();
t2.start();
@@ -43,76 +34,62 @@ public class ThreadSyncDemo {
/*
* 静态方法锁
*/
Thread tt1=new Thread(){
public void run(){
dosome();
}
};
Thread tt2=new Thread(){
public void run(){
dosome();
}
};
Thread tt1 = new Thread(ThreadSyncDemo::doSome);
Thread tt2 = new Thread(ThreadSyncDemo::doSome);
tt1.start();
tt2.start();
/*
* 互斥
*/
final Foo f=new Foo();//保证对象一致
Thread ttt1=new Thread(){
public void run(){
f.methodA();
}
};
Thread ttt2=new Thread(){
public void run(){
f.methodB();
}
};
final Foo f = new Foo();//保证对象一致
Thread ttt1 = new Thread(f::methodA);
Thread ttt2 = new Thread(f::methodB);
ttt1.start();
ttt2.start();
/**
* arraylist,linkedlist,hashset都不是线程安全的
* 线程安全的list集合有一个vector
* hashmap不是线程安全的,线程安全的是hashtable
* collections提供类相应的静态方法,可以将现有的集合或map转换为线程安全的
*/
List<String> list=new ArrayList<String>();
List<String> list = new ArrayList<>();
list.add("one");
list.add("two");
list=Collections.synchronizedList(list);//将现有的list集合转换为线程安全的集合
Set<String> set=new HashSet<String>(list);
set=Collections.synchronizedSet(set); //转换为线程安全的set集合
Map<String,Integer> map=new HashMap<String,Integer>();
list = Collections.synchronizedList(list);//将现有的list集合转换为线程安全的集合
Set<String> set = new HashSet<>(list);
set = Collections.synchronizedSet(set); //转换为线程安全的set集合
Map<String, Integer> map = new HashMap<>();
map.put("语文", 99);
map.put("数学", 95);
map.put("英语文", 58);
map=Collections.synchronizedMap(map);//转换为线程安全的map
map = Collections.synchronizedMap(map);//转换为线程安全的map
}
/*
* 静态方法锁:
* synchronized修饰静态方法肯定同步,因为静态方法只有一份。<br/>
* synchronized修饰静态方法肯定同步,因为静态方法只有一份
*/
public synchronized static void dosome(){
Thread t=Thread.currentThread();
System.out.println(t+"正在执行dosome");
private synchronized static void doSome() {
Thread t = Thread.currentThread();
System.out.println(t + "正在执行doSome");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(t+"执行dosome完毕");
System.out.println(t + "执行doSome完毕");
}
}
class Shop{
public void buy(){
Thread t=Thread.currentThread();
class Shop {
void buy() {
Thread t = Thread.currentThread();
try {
System.out.println(t+"买");
System.out.println(t + "买");
Thread.sleep(5000);
/*
* 同步代码块:
@@ -121,39 +98,41 @@ class Shop{
* 通常使用this即可
*/
synchronized (this) {
System.out.println(t+"试");
System.out.println(t + "试");
Thread.sleep(5000);
}
System.out.println(t+"over");
System.out.println(t + "over");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Foo{
class Foo {
/*
* 互斥锁
* synchronized修饰的是两段代码,但是锁对象相同
* 那么这两端代码就是互斥的
*/
public synchronized void methodA(){
Thread t=Thread.currentThread();
System.out.println(t+"开始执行a");
synchronized void methodA() {
Thread t = Thread.currentThread();
System.out.println(t + "开始执行a");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
e.printStackTrace();
}
System.out.println(t+"a结束");
System.out.println(t + "a结束");
}
public synchronized void methodB(){
Thread t=Thread.currentThread();
System.out.println(t+"开始执行B");
synchronized void methodB() {
Thread t = Thread.currentThread();
System.out.println(t + "开始执行B");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
e.printStackTrace();
}
System.out.println(t+"B结束");
System.out.println(t + "B结束");
}
}