方法1:
1、常见Runnable对象设置同步代码run运行体
class AutoSaleTicket implements Runnable {
private int ticket = 20;
public void run() {
while (true) {// 循环是指线程不停的去卖票
// 当操作的是共享数据时,用同步代码块进行包围起来,这样在执行时,只能有一个线程执行同步代码块里面的内容
synchronized (this) {
if (ticket > 0) {
// 不要在同步代码块里面sleep,作用只是自已不执行,也不让线程执行
System.out.println("lgq"+Thread.currentThread().getName()
+ " 卖出 第 " + (20 - ticket + 1) + " 张票");
ticket--;
} else {
break;
}
}
// 所以把sleep放到同步代码块的外面,这样卖完一张票就休息一会,让其他线程再卖,这样所有的线程都可以卖票
try {
Thread.sleep(200);
} catch (Exception ex) {
}
}
}
}
2、创建多线程,启动多线程
AutoSaleTicket ticket = new AutoSaleTicket();
Thread t1 = new Thread(ticket, "11东城代售");
Thread t2 = new Thread(ticket, "22西城代售");
Thread t3 = new Thread(ticket, "33朝阳代售");
Thread t4 = new Thread(ticket, "44海淀代售");
t1.start();
t2.start();
t3.start();
t4.start();
3、多线程同步执行结果
03-22 15:40:43.167 9967-10272/com.tianxin.httpheader I/System.out: lgq11东城代售 卖出 第 1 张票
03-22 15:40:43.167 9967-10274/com.tianxin.httpheader I/System.out: lgq33朝阳代售 卖出 第 2 张票
03-22 15:40:43.167 9967-10275/com.tianxin.httpheader I/System.out: lgq44海淀代售 卖出 第 3 张票
03-22 15:40:43.167 9967-10273/com.tianxin.httpheader I/System.out: lgq22西城代售 卖出 第 4 张票
03-22 15:40:43.368 9967-10272/com.tianxin.httpheader I/System.out: lgq11东城代售 卖出 第 5 张票
03-22 15:40:43.370 9967-10273/com.tianxin.httpheader I/System.out: lgq22西城代售 卖出 第 6 张票
03-22 15:40:43.370 9967-10274/com.tianxin.httpheader I/System.out: lgq33朝阳代售 卖出 第 7 张票
03-22 15:40:43.371 9967-10275/com.tianxin.httpheader I/System.out: lgq44海淀代售 卖出 第 8 张票
03-22 15:40:43.570 9967-10273/com.tianxin.httpheader I/System.out: lgq22西城代售 卖出 第 9 张票
03-22 15:40:43.571 9967-10275/com.tianxin.httpheader I/System.out: lgq44海淀代售 卖出 第 10 张票
03-22 15:40:43.572 9967-10272/com.tianxin.httpheader I/System.out: lgq11东城代售 卖出 第 11 张票
03-22 15:40:43.572 9967-10274/com.tianxin.httpheader I/System.out: lgq33朝阳代售 卖出 第 12 张票
03-22 15:40:43.771 9967-10273/com.tianxin.httpheader I/System.out: lgq22西城代售 卖出 第 13 张票
03-22 15:40:43.772 9967-10275/com.tianxin.httpheader I/System.out: lgq44海淀代售 卖出 第 14 张票
03-22 15:40:43.773 9967-10272/com.tianxin.httpheader I/System.out: lgq11东城代售 卖出 第 15 张票
03-22 15:40:43.773 9967-10274/com.tianxin.httpheader I/System.out: lgq33朝阳代售 卖出 第 16 张票
03-22 15:40:43.973 9967-10273/com.tianxin.httpheader I/System.out: lgq22西城代售 卖出 第 17 张票
03-22 15:40:43.973 9967-10275/com.tianxin.httpheader I/System.out: lgq44海淀代售 卖出 第 18 张票
03-22 15:40:43.974 9967-10272/com.tianxin.httpheader I/System.out: lgq11东城代售 卖出 第 19 张票
03-22 15:40:43.974 9967-10274/com.tianxin.httpheader I/System.out: lgq33朝阳代售 卖出 第 20 张票
方法2线程池使用:
创建
class MyTask implements Runnable { private int taskNum; public MyTask(int num) { this.taskNum = num; } @Override public void run() { System.out.println("lgq正在执行task "+taskNum); try { Thread.currentThread().sleep(4000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("lgqtask "+taskNum+"执行完毕"); } }
执行
ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 10, 200, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(5)); for(int i=0;i<15;i++){ MyTask myTask = new MyTask(i); executor.execute(myTask); System.out.println("lgq线程池中线程数目:"+executor.getPoolSize()+",队列中等待执行的任务数目:"+ executor.getQueue().size()+",已执行玩别的任务数目:"+executor.getCompletedTaskCount()); } executor.shutdown();
结果