多线程的Demo
在网上摘要了些关于多线程通信的文章,以供后面学习可用
public class ThreadDemo
{
public static void main(String[] args)
{
// demo1();
// demo2();
demo3();
}
/**
* 描述:
* 日期:2018年3月29日
* 作者:HF-TianGenGen
*/
private static void demo1()
{
Thread A = new Thread(new Runnable()
{
@Override
public void run()
{
printNumber("A");
}
});
Thread B = new Thread(new Runnable()
{
@Override
public void run()
{
printNumber("B");
}
});
A.start();
B.start();
}
/**
* A 1, A 2, A 3, B 1, B 2, B 3
*
* A.join() 方法会让 B 一直等待直到 A 运行完毕。
*/
private static void demo2()
{
Thread A = new Thread(new Runnable()
{
@Override
public void run()
{
printNumber("A");
}
});
Thread B = new Thread(new Runnable()
{
@Override
public void run()
{
System.out.println("B 开始等待 A");
try
{
A.join();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
printNumber("B");
}
});
A.start();
B.start();
}
/**
* A 1, B 1, B 2, B 3, A 2, A 3
*
* 首先创建一个 A 和 B 共享的对象锁 lock = new Object();
当 A 得到锁后,先打印 1,然后调用 lock.wait() 方法,交出锁的控制权,进入 wait 状态;
对 B 而言,由于 A 最开始得到了锁,导致 B 无法执行;直到 A 调用 lock.wait() 释放控制权后, B 才得到了锁;
B 在得到锁后打印 1, 2, 3;然后调用 lock.notify() 方法,唤醒正在 wait 的 A;
A 被唤醒后,继续打印剩下的 2,3。
*/
private static void demo3()
{
Object lock = new Object();
Thread A = new Thread(new Runnable()
{
@Override
public void run()
{
System.out.println("INFO: A 等待锁");
synchronized (lock)
{
System.out.println("INFO: A 得到了锁 lock");
System.out.println("A 1");
try
{
System.out.println("INFO: A 准备进入等待状态,调用 lock.wait() 放弃锁 lock 的控制权");
lock.wait();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println("INFO: 有人唤醒了 A, A 重新获得锁 lock");
System.out.println("A 2");
System.out.println("A 3");
}
}
});
Thread B = new Thread(new Runnable()
{
@Override
public void run()
{
System.out.println("INFO: B 等待锁");
synchronized (lock)
{
System.out.println("INFO: B 得到了锁 lock");
System.out.println("B 1");
System.out.println("B 2");
System.out.println("B 3");
System.out.println("INFO: B 打印完毕,调用 lock.notify() 方法");
lock.notify();
}
}
});
A.start();
B.start();
}
/**
* 描述:CountDownLatch
* 计数器
* 创建一个计数器,设置初始值,CountdownLatch countDownLatch = new CountDownLatch(2);
在 等待线程 里调用 countDownLatch.await() 方法,进入等待状态,直到计数值变成 0;
在 其他线程 里,调用 countDownLatch.countDown() 方法,该方法会将计数值减小 1;
当 其他线程 的 countDown() 方法把计数值变成 0 时,等待线程 里的 countDownLatch.await() 立即退出,继续执行下面的代码。
* 日期:2018年3月29日
* 作者:HF-TianGenGen
*/
private static void demo4()
{
int worker = 3;
CountDownLatch countDownLatch = new CountDownLatch(worker);
new Thread(new Runnable()
{
@Override
public void run()
{
System.out.println("D is waiting for other three threads");
try
{
countDownLatch.await();
System.out.println("All done, D starts working");
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}).start();
for (char threadName = 'A'; threadName <= 'C'; threadName++)
{
final String tN = String.valueOf(threadName);
new Thread(new Runnable()
{
@Override
public void run()
{
System.out.println(tN + "is working");
try
{
Thread.sleep(100);
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println(tN + "finished");
countDownLatch.countDown();
}
}).start();
}
}
private static void printNumber(String threadName)
{
int j = 0;
while (j++ < 3)
{
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println(threadName + " print: " + j);
}
}
}
浙公网安备 33010602011771号