Java_多线程(创建/同步锁/等待唤醒机制/线程池/AsyncTask)
本文由 Luzhuo 编写,转发请保留该信息.
原文: http://blog.youkuaiyun.com/Rozol/article/details/77344792
本文讲解了: 线程的创建 / 同步锁机制 / 等待唤醒机制 / 线程池 / Android的AsyncTask源码分析
线程的创建
创建子线程大约可分为4中方式
public class Test {
public static void main(String[] args) {
// 方式一: 继承Thread类
run1();
// 方式二: 重写Thread的run方法
run2();
// 方式三: 实现Runnable接口
run3();
// 方式四: 实现Runnable接口2
run4();
}
/**
* 方式一: 继承Thread类
*/
private static void run1() {
MyThreadClass thread1_1 = new MyThreadClass();
MyThreadClass thread1_2 = new MyThreadClass("thread 1_2");
thread1_1.setName("thread 1_1");
thread1_1.start();
thread1_2.start();
}
/**
* 方式二: 重写Thread的run方法
*/
private static void run2() {
new Thread("thread 2"){
public void run() {
for (int x = 0; x < 100; x++) {
System.out.println(Thread.currentThread().getName() + ": " + x);
}
};
}.start();
}
/**
* 方式二: 实现Runnable接口
*/
private static void run3() {
Thread thread2_1 = new Thread(new MyRunnable());
thread2_1.setName("thread 3_1");
thread2_1.start();
Thread thread2_2 = new Thread(new MyRunnable(), "thread 3_2");
thread2_2.start();
}
/**
* 方式三: 实现Runnable接口2
*/
private static void run4() {
new Thread(new Runnable() {
@Override
public void run() {
for (int x = 0; x < 100; x++) {
System.out.println(Thread.currentThread().getName() + ": " + x);
}
}
}, "thread 4").start();
}
}
public class MyThreadClass extends Thread{
public MyThreadClass(){};
public MyThreadClass(String name){
super(name);
};
@Override
public void run(){ //继承Thread类并重写run()方法
for(int x = 0; x < 100; x++){
//public final String getName(); //返回该线程的名称
System.out.println(getName() + ": " + x);
}
}
}
public class MyRunnable implements Runnable{
@Override
public void run() {
for (int x = 0; x < 100; x++) {
//public static Thread currenthread(); //返回对当前正在执行的线程对象的引用
System.out.println(Thread.currentThread().getName() + ": " + x);
}
}
}
线程的同步锁
同步锁实现方式
/**
* 同步锁的几种实现放方式
* 区别:
* Synchronized: 采用CPU悲观锁机制(JVM执行), 线程是独占的, 当很多线程进程锁时会引起CPU频繁切换而影响性能
* Lock: java写的乐观锁, 每次不加锁假设没有冲突去执行, 如果发生冲突则重试
* @author Luzhuo
*/
public class Test {
public static void main(String[] args) {
// 方式1: 同步代码块
SynchronizedBlock block = new SynchronizedBlock();
Thread thread1_1 = new Thread(block, "1号窗口");
Thread thread1_2 = new Thread(block, "2号窗口");
thread1_1.start();
thread1_2.start();
// 方式2: 同步方法
SynchronizedMethod method = new SynchronizedMethod();
Thread thread2_1 = new Thread(method, "1号窗口");
Thread thread2_2 = new Thread(method, "2号窗口");
thread2_1.start();
thread2_2.start();
// 方式3: 静态同步方法
SynchronizedStaticMethod staticMethod = new SynchronizedStaticMethod();
Thread thread3_1 = new Thread(staticMethod, "1号窗口");
Thread thread3_2 = new Thread(staticMethod, "2号窗口");
thread3_1.start();
thread3_2.start();
// 方式4: Lock锁
LockBlock lockBlock = new LockBlock();
Thread thread4_1 = new Thread(lockBlock, "1号窗口");
Thread thread4_2 = new Thread(lockBlock, "2号窗口");
thread4_1.start();
thread4_2.start();
// ==========================================================
// 数据安全访问的几种方式
DataSecurity data = new DataSecurity();
Thread thread5_1 = new Thread(data, "thread 1");
Thread thread5_2 = new Thread(data, "thread 2");
thread5_1.start();
thread5_2.start();
}
}
/**
* 同步代码块
* @author Luzhuo
*/
public class SynchronizedBlock implements Runnable{
private static int x = 100; // 票的数量
private Object obj = new Object();
@Override
public void run() {
while (true) {
syncBlock();
}
}
/**
* 方式1: 同步代码块, 锁对象:任意对象
*/
public void syncBlock() {
// synchronized代码同步锁
synchronized (obj) { // 锁对象是new Object();
if (x > 0) {
try {
// public static void sleep(long millis); // 以指定毫秒数内暂停线程
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "正在出售第 " + (x--) + " 张票");
}
}
}
}
/**
* 同步方法
* @author Luzhuo
*/
public class SynchronizedMethod implements Runnable{
private static int x = 100; // 票的数量
@Override
public void run() {
while(true){
syncMethod();
}
}
/**
* 方式2: 同步方法: 锁对象:this
*/
private synchronized void syncMethod() { //锁对象是 this
if (x > 0) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "正在出售第 " + (x--) + " 张票");
}
}
}
/**
* 静态同步方法
* @author Luzhuo
*
*/
public class SynchronizedStaticMethod implements Runnable{
private static int x = 100; // 票的数量
@Override
public void run() {
while(true){
syncStaticMethod();
}
}
/**
* 方式3: 静态同步方法: 锁对象: 类.class 字节码文件对象
*/
private static synchronized void syncStaticMethod(){ //锁对象是 Ticket.class
if (x > 0) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "正在出售第 " + (x--) + " 张票");
}
}
}
/**
* Lock是Java5之后加入的
* @author Luzhuo
*/
public class LockBlock implements Runnable{
private int x = 100; // 票的数量
@Override
public void run() {
while (true) {
lockBlock();
}
}
// 定义一个锁对象
private final Lock lock = new ReentrantLock();
/**
* 方式4: Lock锁代码块
*/
public void lockBlock() {
// void lock(); //获取锁
lock.lock();
try{
if (x > 0) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "正在出售第 " + (x--) + " 张票");
}
}finally {
// void unlock(); //释放锁
lock.unlock();
}
}
}
死锁
/**
* 死锁的案例
* 死锁的产生: 线程1拿着objA的锁去获取objB的锁, 线程2拿着objB的锁去获取objA的锁, 两者互不相让就产生了死锁
* @author Luzhuo
*/
public class 死锁{
public static void main(String[] args) {
死锁.DieLock d1 = new 死锁.DieLock(true);
死锁.DieLock d2 = new 死锁.DieLock(false);
d1.start();
d2.start();
}
public static class DieLock extends Thread{
private boolean flag;
public DieLock(boolean flag){
this.flag = flag;
}
public void run() {
if(flag){
while(true){
synchronized (Mylock.objA) {
System.out.println("if objA");
synchronized (Mylock.objB) {
System.out.println("if objB");
}
}
}
}else{
while(true){
synchronized (Mylock.objB) {
System.out.println("else objB");