nefu 627 剪纸游戏

--用wait notifyAll来实现生产者与消费者模式,如下

package com.collonn.procon2;

import java.util.LinkedList;
import java.util.concurrent.atomic.AtomicInteger;

public class PCTest {
	// the max number of product in product pool
	public static final int QUEUE_MAX_SIZE = 3;
	// product pool
	public static final LinkedList<Integer> QUEUE = new LinkedList<Integer>();
	// product name
	public static final AtomicInteger ATOMIC_INTEGER = new AtomicInteger();

	// the speed of producer
	public static final int PRODUCE_SPEED = 1000 * 1;
	// the speed of consumer
	public static final int CONSUME_SPEED = 100 * 1;

	// the number of producer
	public static final int PRODUCE_COUNT = 1;
	// the number of consumer
	public static final int CONSUME_COUNT = 5;

	// create producer
	private void produce() {
		for (int i = 0; i < PRODUCE_COUNT; i++) {
			new Thread(new Producer()).start();
		}
	}

	// create consumer
	private void consume() {
		for (int i = 0; i < CONSUME_COUNT; i++) {
			Thread thread = new Thread(new Consumer());
			thread.setName("td" + i);
			thread.start();
		}
	}

	public static void main(String[] args) throws Exception {
		PCTest pct1 = new PCTest();
		System.out.println("start producer...");
		pct1.produce();
		Thread.sleep(1000 * 5);
		System.out.println("start consumer...");
		pct1.consume();
	}

}

// producer
class Producer implements Runnable {

	@Override
	public void run() {
		while (true) {
			try {
				synchronized (PCTest.QUEUE) {
					if(PCTest.QUEUE.size() == PCTest.QUEUE_MAX_SIZE){
						System.out.println("product pool full...");
						PCTest.QUEUE.wait();
					}else{
						int next = PCTest.ATOMIC_INTEGER.getAndAdd(1);
						PCTest.QUEUE.addLast(next);
						System.out.println("produce," + next);

						PCTest.QUEUE.notifyAll();
					}
				}

				Thread.sleep(PCTest.PRODUCE_SPEED);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}

// consumer
class Consumer implements Runnable {

	@Override
	public void run() {
		try {
			while (true) {
				synchronized (PCTest.QUEUE) {
					if(PCTest.QUEUE.size() == 0){
						System.out.println("product pool empty...");
						PCTest.QUEUE.wait();
					}else{
						Integer data = PCTest.QUEUE.removeFirst();
						System.out.println("consume," + Thread.currentThread().getName() + "," + data);

						PCTest.QUEUE.notifyAll();
					}
				}

				Thread.sleep(PCTest.CONSUME_SPEED);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}

--用BlockingDeque来实现生产者与消费者模式,如下

package com.collonn.procon;

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.atomic.AtomicInteger;

public class ProConMain {
	// product pool
	public static final BlockingDeque<Integer> BLOCK_QUEUE = new LinkedBlockingDeque<Integer>(5);
	// product name
	public static final AtomicInteger ATOMIC_INTEGER = new AtomicInteger();

	// the speed of producer
	public static final int PRODUCE_SPEED = 1000 * 3;
	// the speed of consumer
	public static final int CONSUME_SPEED = 1000 * 1;

	// the number of producer
	public static final int PRODUCE_COUNT = 1;
	// the number of consumer
	public static final int CONSUME_COUNT = 20;

	// create producer
	private void produce() {
		for (int i = 0; i < PRODUCE_COUNT; i++) {
			new Thread(new Producer()).start();
		}
	}

	// create consumer
	private void consume() {
		for (int i = 0; i < CONSUME_COUNT; i++) {
			Thread thread = new Thread(new Consumer());
			thread.setName("td" + i);
			thread.start();
		}
	}

	public static void main(String[] args) {
		ProConMain t1 = new ProConMain();
		t1.produce();
		t1.consume();
	}

}

// producer
class Producer implements Runnable {

	@Override
	public void run() {
		while (true) {
			try {
				int next = ProConMain.ATOMIC_INTEGER.getAndAdd(1);
				ProConMain.BLOCK_QUEUE.putLast(next);
				System.out.println("produce," + next);

				Thread.sleep(ProConMain.PRODUCE_SPEED);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}

// consumer
class Consumer implements Runnable {

	@Override
	public void run() {
		try {
			while (true) {
				Integer data = ProConMain.BLOCK_QUEUE.takeFirst();
				System.out.println("consume," + Thread.currentThread().getName() + "," + data);

				Thread.sleep(ProConMain.CONSUME_SPEED);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}

一:用Executor来实现生产者与消费者模式,如下

package com.collonn.executor;

import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class ProConExecutor {
	// product name
	public static final AtomicInteger ATOMIC_INTEGER = new AtomicInteger();

	// the speed of producer
	public static final int PRODUCE_SPEED = 1000 * 1;
	// the speed of consumer
	public static final int CONSUME_SPEED = 1000 * 5;

	// the speed of consumer
	public static final int BLOCK_QUEUE_SIZE = 5;

	// create thread pool with deal strategy
	// corePoolSize, maximumPoolSize, keepAliveTime, timeUnit, workQueue, rejectedExecutionHandler
	public static final Executor EXECUTOR = new ThreadPoolExecutor(1, 3, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(BLOCK_QUEUE_SIZE),
			new RejectedExecutionHandler() {
				@Override
				public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
					System.out.println("[overload], ignore data:" + ((Taskk)r).getData() + ", poolSize:" + e.getPoolSize() + ", queueSize:" + e.getQueue().size());
				}
			});

	public static void main(String[] args) {
		// produce product, then, throw to thead pool, then deal the data
		new Thread() {
			@Override
			public void run() {
				try {
					while (true) {
						int next = ProConExecutor.ATOMIC_INTEGER.getAndAdd(1);
						ProConExecutor.EXECUTOR.execute(new Taskk(next));
						System.out.println("produce," + next);

						Thread.sleep(PRODUCE_SPEED);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}

}

// process data
class Taskk implements Runnable {
	private Integer data;

	public Taskk(Integer data) {
		this.data = data;
	}

	public Integer getData() {
		return data;
	}

	public void setData(Integer data) {
		this.data = data;
	}

	@Override
	public void run() {
		try {
			System.out.println("consume," + this.data);

			Thread.sleep(ProConExecutor.CONSUME_SPEED);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

总结:我们可以看到,代码在一步步的精简,且更优雅。

nefu 627 剪纸游戏

时间: 2024-08-04 05:06:13

nefu 627 剪纸游戏的相关文章

移动时代的前端加密

移动时代的前端加密 标签: 加密 前端 HTML5 移动 背景 相比其它被编译成二进制的应用.前端这样的纯文本应用,太easy被解读和窜改. 前端为什么要加密? 加密重要的目的是出于对商业利益的保护. 因为作品太easy被复制窜改.easy会失去渠道先机 窜改不限于下面: 署名被移除或替换: 链接地址被替换: 文案被改动: 广告被移除.替换或植入: - 一些轻度游戏,用户仅仅会玩一两次,生命周期也就两三天.假设你开发的游戏被人山寨且他的渠道比你更广,那么对于流量就是致命打击. HTML5 被山寨

游戏引擎架构学习

本书的主页第一部分 基础 第1章 导论1.1 典型游戏团队的结构 工程师:艺术家:游戏设计师:制作人:其他工作人员:发行商&工作室 1.2 游戏是什么 71.3 游戏引擎是什么 101.4 不同游戏类型中的引擎差异 111.5 游戏引擎概观 221.6 运行时引擎架构 271.7 工具及资产管道 46第2章 专业工具 532.1 版本控制 532.2 微软Visual Studio 612.3 剖析工具 782.4 内存泄漏和损坏检测 792.5 其他工具 80第3章 游戏软件工程基础 833.

天猫 小游戏 24 point

游戏规则:给你四个整数,当然他给的是有解的,然后用' +  -  *   /   (  )   ,这几种符号任意组合,使运算结果等于24; 用代码快速解决问题,呵呵... 1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstring> 5 #include<queue> 6 #include<string> 7 #include<

控制台小游戏-贪吃蛇,c++和c#版

说是c++版,其实只是用到了c++的cout和cin而已.这是我做的第二个控制台游戏,基本上每一行代码都加上了注释. 游戏嘛,我觉得重要的是了解他的思想,所以后期学了面向对象之后这个游戏的代码我也没有重新封装. 下面请看图 代码如下:我是用dev c++写的 1 //注释. ---星辰 2 3 #include <iostream> 4 #include<Windows.h> 5 #include<ctime> 6 #include<cstdlib> 7 #

Java实现贪吃蛇游戏【代码】

花了两个下午写了一个贪吃蛇小游戏,本人想写这游戏很长时间了.作为以前诺基亚手机上的经典游戏,贪吃蛇和俄罗斯方块一样,都曾经在我们的童年给我们带来了很多乐趣.世间万物斗转星移,诺基亚曾经作为手机业的龙头老大,现如今也一步步走向衰落,被收购,再过不久估计就要退出手机业务了,而贪吃蛇这款游戏也基本上没人玩了,甚至在新一代人的印象中都已毫无记忆了...但是,这款游戏在它基础上经过改造其实可以弄出很多花样,也确实可以在一定程度上锻炼自己的编程能力.前不久十分火热的贪吃蛇大作战其实就可以看做是在这款游戏的基

HTML5游戏源码 飞翔的字母 可自定义内容

相信大家都玩过飞翔的小鸟吧,当然,可能已经有很多人因为这个游戏砸了不少手机.吼吼. 废话不多说,回到主题,源码如下,需要打包源码的朋友们请留言邮箱地址.当然还有,不要忘了点赞哦~谢谢大家的支持. 直接上源码:一共是三个文件:页面.js.css. HTML(index.html)页面源码如下: 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title&g

Html5 Canvas斗地主游戏

过完年来公司,没什么事,主管说研究下html5 游戏,然后主管就给了一个斗地主的demo,随后我就开始看代码, 现在我看了html5以及canvas相关知识和斗地主的demo后,自己用demo上的素材试着写了个斗地主,代码没重构好,欢迎赐教. 演示地址:CanVas斗地主 话不多说,下面就一步一步解释下吧 只有一个common.js文件 1.资源类 1 var Resource = Class.create(); 2 $.extend(Resource.prototype, { 3 initia

[原创]html5游戏_五线谱打音符

html5手机游戏—五线谱打音符 1.[用五线谱打唱名] 2.[用唱名打五线谱] 3.[无限练习模式] 用来熟悉五线谱上音符的位置 代码不难,这回注释还是有认真写的[只是废代码没有全部删除...] 效果图: --- 在线地址: http://wangxinsheng.herokuapp.com/staffgame --- 代码: index.html 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="

C++的简单“五子棋”游戏,只是核心代码,资源代码未添加

ChessBoard.h 1 #ifndef __CHESS_BOARD_H__ 2 #define __CHESS_BOARD_H__ 3 4 #include "DataStruct.h" 5 6 #define COL_WIDTH 45 7 #define ROW_WIDTH 45 8 9 class CChessBoard : public CWnd 10 { 11 private: 12 CBitmap m_bitBlackChess, m_bitWhiteChess; 13