`
yanguz123
  • 浏览: 556062 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

\(^_^)/ Java多线程Guarded-Suspension模式

    博客分类:
  • Code
 
阅读更多

第一种

 

1.请求

package guarded_suspension;

public class Request {
	// 模拟请求内容
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Request(String name) {
		this.name = name;
	}

}

 

2.请求队列

package guarded_suspension;

import java.util.LinkedList;

public class RequestQueue {
	private LinkedList queue = new LinkedList();

	public synchronized Request getRequest() {
		while (queue.size() == 0) {
			try {
				wait(); // 等待,直到有新的request加入
			} catch (InterruptedException ex) {

			}
		}
		// 返回request队列中的第一个请求
		return (Request) queue.remove();
	}

	public synchronized void addRequest(Request request) {
		// 加入新的request请求
		queue.add(request);
		// 通知getRequest方法
		notifyAll();
	}
}

 

3.Server端

package guarded_suspension;

public class ServerThread extends Thread {
	// 请求队列
	private RequestQueue requestQueue;

	public ServerThread(RequestQueue requestQueue, String name) {
		super(name);
		this.requestQueue = requestQueue;
	}

	public void run() {
		while (true) {
			// 得到请求
			final Request request = requestQueue.getRequest();
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName() + " handle " + request);
		}
	}
}

 

4.Client端

package guarded_suspension;

public class ClientThread extends Thread {
	// 请求队列
	private RequestQueue requetQueue;

	public ClientThread(RequestQueue requestQueue, String name) {
		super(name);
		this.requetQueue = requestQueue;
	}

	public void run() {
		for (int i = 0; i < 10; i++) {
			Request request = new Request("RequestId " + i + " Thread Name " + Thread.currentThread().getName());
			System.out.println(Thread.currentThread().getName() + " requests " + request);
			requetQueue.addRequest(request);
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("Client Thread Name is " + Thread.currentThread().getName());
		}
		System.out.println(Thread.currentThread().getName() + " request end.");
	}
}

 

5.测试类

package guarded_suspension;

public class Test {
	public static void main(String[] args) {
		RequestQueue requestQueue = new RequestQueue();
		for (int i = 0; i < 10; i++) {
			new ServerThread(requestQueue, "ServerThread-" + i).start();
		}
		for (int i = 0; i < 10; i++) {
			new ClientThread(requestQueue, "ClientThread-" + i).start();
		}
	}
}

 

 

 

 

 

 

 

 

 

 

 

第二种

有返回值... ...

分享到:
评论

相关推荐

    java多线程设计模式详解(PDF及源码)

    (注意,本资源附带书中源代码可供参考) 多线程与并发处理是程序设计好坏优劣的重要课题,本书通过浅显易懂的文字与实例来介绍Java线程相关的设计模式概念,并且通过实际的Java程序范例和 UML图示来一一解说,书中...

    java多线程设计模式 (PDF中文版, 附源码)

    目录: 漫谈UML Introduction 1 Java语言的线程 Introduction 2 多线程...总结 多线程程序设计的模式语言 附录A 练习问题的解答 附录B Java的内存模型 附录C Java线程的优先级 附录D 线程相关的主要API 附录E 参考文献

    Java多线程详解

    Java多线程模式详解 目录: 一、漫谈UML Java语言的线程 多线程的评量标准 二、 1、Single Threaded Execution ———— 能通过这座桥的,只有一个人 2、Immutable ———— 想破坏它也没办法 3、Guarded ...

    guarded-array:边界检查数组

    var guard = require ( 'guarded-array' ) //First create any old array var array = [ 0 , 1 , 2 , 3 , 4 , 5 ] //Then we protect it using guard! var guardedArray = guard ( array ) //The guarded array ...

    guarded-string:防止在应用程序中意外地在字符串中引入XSSKong

    yarn add guarded-string 用法 重要的! 应该将其用于防止XSS攻击之类的东西,而不是用于隐藏敏感信息。 import guardedString from 'guarded-string' ; const myString = guardedString `My very important (but ...

    36种最新设计模式整理

    Guarded Suspension 模式 Producer Consumer 模式 Worker Thread 模式 Thread-Per-Message 模式 Future 模式 Read-Write-Lock 模式 Two-phase Termination 模式 Thread-Specific Storage 模式

    guarded-bayou-7383

    JAX-RS 模板应用程序这是使用 JAX-RS 的轻量级 RESTful API 的模板。...在本地运行应用程序首先构建: $mvn clean install然后运行它: $ java -cp target/classes:target/dependency/* com.example.Main

    节律

    目录安装将文件从github repo复制到所需目录,在目录中运行“ npm install”用法npm开始可以在此处查看该应用程序的示例运行: [inRHYTHM Heroku Link](https://guarded-cliffs-86649.herokuapp.com/)问题GitHub档案...

    汪文君高并发编程实战视频资源全集

    │ 高并发编程第二阶段38讲、多线程Active Objects设计模式(接受异步消息的主动对象)-上.mp4 │ 高并发编程第二阶段39讲、多线程Active Objects设计模式(接受异步消息的主动对象)-中.mp4 │ 高并发编程第二阶段40...

    汪文君高并发编程实战视频资源下载.txt

    │ 高并发编程第二阶段38讲、多线程Active Objects设计模式(接受异步消息的主动对象)-上.mp4 │ 高并发编程第二阶段39讲、多线程Active Objects设计模式(接受异步消息的主动对象)-中.mp4 │ 高并发编程第二阶段40...

    courses-store-express:Node.JS和Express.Handlebars的关系

    Ссылканапроект: ://guarded-lake-27673.herokuapp.com/ Описание Node.JS和Express把手。 说明: регистрацияпользователей авторизация сме...

    scalac-guardedblocks-plugin:简单的Scala编译器插件

    scalac-guardedblocks-plugin 简单的Scala编译器插件

    heroku-destroy-temp:Heroku CLI插件可销毁临时应用

    3. guarded-journey-99652 4. floating-atoll-86710 5. obscure-anchorage-63377 Destroy these 5 apps? (y/n): y destroying apps... done 安装 $ heroku plugins:install heroku-destroy-temp

    laravel中的fillable和guarded属性详解

    今天小编就为大家分享一篇laravel中的fillable和guarded属性详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

    butterknife 8.8.0

    When specified as false, checks for required views being non-null are elided and casts are no longer guarded with user-friendly error messages. This reduces the amount of generated code for release ...

    微软内部资料-SQL性能优化3

    An isolation level determines the degree to which data is isolated for use by one process and guarded against interference from other processes. Prior to SQL Server 7.0, REPEATABLE READ and ...

    解决Laravel 使用insert插入数据,字段created_at为0000的问题

    据官方文档的说明,使用Eloquent ORM,插数据库的时候可以自动生成created_at,... protected $guarded = []; //获取部门名称 public function fromDep(){ return $this-&gt;belongsTo('App\Models\Department','from',

    filter-anything:“ pick”和“ omit”的简单(TypeScript)集成,用于过滤对象的道具

    过滤任何东西 :crossed_swords: npm i filter-anything一种过滤掉对象道具的实现,例如TypeScript“ pick”和“ omit”。 在Laravel世界中,这也称为“填充物”和“防护物”。动机我创建此程序包是因为我需要: 能够...

    程序语言设计原理习题解答

    8.5 Guarded Commands 367 8.6 Conclusions 371 Summary • Review Questions • Problem Set •Programming Exercises 372 Chapter 9 Subprograms 377 9.1 Introduction 378 9.2 Fundamentals of ...

Global site tag (gtag.js) - Google Analytics