不考虑多服务器,限制线程池的大小和队列的限制来实现。
代码如下:
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
}
}
import java.util.Scanner
public class Wuziqi {
/**
* 棋盘
*/
private final int[][] qipan
/**
* 步数
*/
private int bushu
/**
* 构造方法,设置棋盘规格
* @param x
* @param y
*/
public Wuziqi(int x, int y) {
if (x <1 || y <1) {
System.out.println("棋盘规格应不小于1,使用默认规格")
qipan = new int[9][9]
} else {
qipan = new int[y][x]
}
}
/**
* 游戏开始
*/
public void play() {
int[] zuobiao = null
//如果游戏没有结束
while (!end(zuobiao)) {
zuobiao = luozi()
//输出棋盘
out()
}
}
/**
* 输出棋盘和棋子
*/
private void out() {
for (int i = 0i <qipan.lengthi++) {
for (int j = 0j <qipan[i].lengthj++) {
if (qipan[i][j] == 0) {
System.out.print(" +")
}else if (qipan[i][j] == -1) {
System.out.print(" 白")
}else if (qipan[i][j] == 1) {
System.out.print(" 黑")
}
}
System.out.println(" ")
}
}
/**
* 落子
*/
private int[] luozi() {
int[] zuobiao
bushu++
if (bushu % 2 == 1) {
System.out.println("请黑方落子")
zuobiao = input()
qipan[zuobiao[1]][zuobiao[0]] = 1
}else {
System.out.println("请白方落子")
zuobiao = input()
qipan[zuobiao[1]][zuobiao[0]] = -1
}
return zuobiao
}
/**
* 输入坐标
* @return
*/
private int[] input() {
Scanner sc = new Scanner(System.in)
System.out.println("请输入x轴坐标")
String x = sc.next()
System.out.println("请输入y轴坐标")
String y = sc.next()
//如果没有通过验证,则再次执行input(),递归算法
if (!validate(x, y)) {
return input()
}
int int_x = Integer.valueOf(x)
int int_y = Integer.valueOf(y)
return new int[] {int_x, int_y}
}
/**
* 校验数据
* @param x
* @param y
* @return
*/
private boolean validate(String x, String y) {
Integer int_x = null
Integer int_y = null
//异常处理的方式判断字符串是否是一个整数
try {
int_x = Integer.valueOf(x)
int_y = Integer.valueOf(y)
} catch (NumberFormatException e) {
System.out.println("坐标格式错误,坐标应为整数")
return false
}
if (int_x <0 || int_y <0 || int_x >= qipan[0].length || int_y >= qipan.length) {
System.out.println("坐标越界")
return false
}
if (qipan[int_y][int_x] == 0) {
return true
} else {
System.out.println("坐标上已有棋子")
}
return false
}
/**
* 结束条件
* @return
*/
private boolean end(int[] zuobiao) {
if (zuobiao == null) {
return false
}
//计数器
//表示棋盘上经过最近落子坐标的4条线上的连续(和最近落子颜色相同的)棋子的个数
//如果某条线上连续的棋子大于等于4(加上最近落子本身,大于等于5),则游戏结束,符合五子棋规则
int[] jieguo = new int[4]
int x = zuobiao[0]
int y = zuobiao[1]
//定义八个方向
final int[][] fangxiang = {{-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}}
//最近落子的坐标上的棋子颜色
int number = qipan[y][x]
//搜索最近落子坐标为中心最远4的距离
for (int i = 1i <= 4i++) {
//每次搜索不同的距离都搜索八个方向
for (int j = 0j <fangxiang.lengthj++) {
//约定如果某个方向为null时,不再搜索这个方向。关键字continue是跳过本次(一次)循环的意思
if (fangxiang[j] == null) {
continue
}
int mubiao_x = x + i * fangxiang[j][0]
int mubiao_y = y + i * fangxiang[j][1]
//如果搜索坐标相对于棋盘越界,则不再搜索这个方向
if (mubiao_y >= qipan.length || mubiao_y <0 || mubiao_x >= qipan[0].length || mubiao_x <0) {
fangxiang[j] = null
continue
}
//如果最近落子坐标上的值等于目标坐标上的值(颜色相同),则计数器上某条线加1
//否则认为这个方向没有棋子或有别的颜色的棋子,不再搜索这个方向
if (number == qipan[mubiao_y][mubiao_x]) {
jieguo[j % 4]++
}else {
fangxiang[j] = null
}
}
}
//查看计数器上是否有比3更大的数(查看是否有一方胜出)
for (int i : jieguo) {
if (i >3) {
System.out.println("游戏结束")
if (bushu % 2 == 1) {
System.out.println("黑方胜")
} else {
System.out.println("白方胜")
}
return true
}
}
//没有胜出者的情况下,查看棋盘上是否还有空位置,如果有,则游戏可以继续
for (int[] arr : qipan) {
for (int i : arr) {
if (i == 0) {
return false
}
}
}
//如果没有空位置,则平局
System.out.println("游戏结束,平局")
return true
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)