不考虑多服务器,限制线程池的大小和队列的限制来实现。
代码如下:
package org.zhang
import java.util.concurrent.BlockingQueue
import java.util.concurrent.Executors
import java.util.concurrent.SynchronousQueue
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.TimeUnit
/**
* 单服务器用线程池实现秒杀的思路一
*
* @author zhanghaijun
*
*/
public class ExecutorsTest {
public static boolean flag = true // 秒杀物品的标记
public static void main(String[] args) {
ThreadPoolExecutor pool = new ThreadPoolExecutor(1, 1, 0L,
TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>())
ThreadTest t1 = new ThreadTest("张三")
ThreadTest t2 = new ThreadTest("李四")
ThreadTest t3 = new ThreadTest("王五")
try {
pool.execute(t1)
} catch (Exception e) {
System.out.println(t1.getUserName() + "没有抢到")
}
try {
pool.execute(t3)
} catch (Exception e) {
System.out.println(t3.getUserName() + "没有抢到")
}
try {
pool.execute(t2)
} catch (Exception e) {
System.out.println(t2.getUserName() + "没有抢到")
}
pool.shutdown()
}
}
class ThreadTest extends Thread {
private String userName
public ThreadTest(String userName) {
super()
this.userName = userName
}
@Override
public void run() {
try {
Thread.sleep(200)
if (ExecutorsTest.flag) {
System.out.println(this.userName + "秒杀成功")
ExecutorsTest.flag = false
}
} catch (InterruptedException e) {
e.printStackTrace()
}
}
public String getUserName() {
return userName
}
public void setUserName(String userName) {
this.userName = userName
}
}
最好不要用java写秒杀器,因为你就算用 httpclient 拿到的也是未经过渲染的html页面,很多页面js都没有加载,你根本不知道渲染之后的页面长什么样子,你最好学学木鱼的火车票抢票助手,他用的是 firefox 的插件 scriptish 来写抢票脚本,其实抢票跟秒杀是一个原理的,我第一个秒的程序就是照着他的程序改的,用这个上手也比较容易,但是要求你对javascript比较熟悉,不过比用java实现靠谱多了欢迎分享,转载请注明来源:内存溢出
评论列表(0条)