本次通过最简单的卖票系统进行演示Thread和Runnable
1、Thread的使用案例
public class ThreadTest extends Thread{
/**
* 通过构造方法给线程赋名
* @param name
*/
public ThreadTest(String name){
super(name);
}
// 总票数
static int tick = 20;
// 锁
static String lock = "ok";
public ThreadTest() {
}
@Override
public void run(){
while (tick > 0){
synchronized (lock){
if (tick > 0){
System.out.println(getName() + "卖出了第" + tick);
tick--;
}else {
System.out.println("票卖完了");
}
}
try {
sleep(1000);
} catch (InterruptedException e){
e.printStackTrace();
}
}
}
}
调用Thread进行测试
public class TestThreadMain {
public static void main(String[] args) {
ThreadTest test1 = new ThreadTest("张三");
ThreadTest test2 = new ThreadTest("李四");
ThreadTest test3 = new ThreadTest("王五");
test1.start();
test2.start();
test3.start();
}
}
调用Thread返回结果
张三卖出了第20
王五卖出了第19
李四卖出了第18
张三卖出了第17
王五卖出了第16
李四卖出了第15
李四卖出了第14
王五卖出了第13
张三卖出了第12
王五卖出了第11
李四卖出了第10
张三卖出了第9
王五卖出了第8
李四卖出了第7
张三卖出了第6
王五卖出了第5
李四卖出了第4
张三卖出了第3
王五卖出了第2
李四卖出了第1
票卖完了
2、Runnable的使用案例
public class RunnableTest implements Runnable{
// 总票数
static int tick = 20;
// 锁
static String lock = "ok";
@Override
public void run() {
while (tick > 0){
synchronized (lock){
if (tick>0){
System.out.println(Thread.currentThread().getName() + "卖出了第" + tick + "票");
tick--;
} else {
System.out.println("卖完了");
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
调用Runnable进行测试
public class TestRunnableMain {
public static void main(String[] args) {
RunnableTest t1 = new RunnableTest();
new Thread(t1,"张三").start();
new Thread(t1,"李四").start();
new Thread(t1,"王五").start();
}
}
调用Runnable返回结果
张三卖出了第20票
王五卖出了第19票
李四卖出了第18票
李四卖出了第17票
王五卖出了第16票
张三卖出了第15票
王五卖出了第14票
李四卖出了第13票
张三卖出了第12票
张三卖出了第11票
王五卖出了第10票
李四卖出了第9票
张三卖出了第8票
王五卖出了第7票
李四卖出了第6票
张三卖出了第5票
王五卖出了第4票
李四卖出了第3票
王五卖出了第2票
李四卖出了第1票
卖完了
3、newCachedThreadPool的使用案例
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CachedThreadPoolTestTwo {
public static void main(String[] args) {
ExecutorService executor = Executors.newCachedThreadPool();
for (int i = 0;i < 30;i++){
int index = i;
executor.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "-------------" + index);
}
});
}
}
}
执行newCachedThreadPool返回结果
pool-1-thread-2-------------1
pool-1-thread-1-------------0 /*线程-1 被调用了 3次 ----->>> 这是第一次*/
pool-1-thread-3-------------2
pool-1-thread-4-------------3
pool-1-thread-5-------------4
pool-1-thread-6-------------5
pool-1-thread-1-------------13 /*线程-1 被调用了 3次 ----->>> 这是第二次*/
pool-1-thread-6-------------14
pool-1-thread-4-------------16
pool-1-thread-3-------------17
pool-1-thread-2-------------18
pool-1-thread-8-------------7
pool-1-thread-1-------------20 /*线程-1 被调用了 3次 ----->>> 这是第三次*/
pool-1-thread-2-------------25
pool-1-thread-9-------------8
pool-1-thread-6-------------23
pool-1-thread-10-------------9
pool-1-thread-11-------------10
pool-1-thread-5-------------15
pool-1-thread-12-------------11
pool-1-thread-3-------------26
pool-1-thread-13-------------12
pool-1-thread-4-------------27
pool-1-thread-2-------------29
pool-1-thread-7-------------6
pool-1-thread-14-------------19
pool-1-thread-15-------------21
pool-1-thread-16-------------22
pool-1-thread-17-------------24
pool-1-thread-18-------------28