Position:Home>JAVA Development> Java line Cheng / the blemish of memory model and increase
Java line Cheng / the blemish of memory model and increase
From;  Author:Stand originally


Use Channel we are OK very easy write the code that has message alignment function, give typical examples is as follows:
Code 4

Package Org.javaresearch.j2seimproved.thread;Import EDU.oswego.cs.dl.util.concurrent. Public Static Void Main(String[] Args) {TestChannel Tc = New TestChannel(); For(int I = 0;i of alignment of information of *;public Class TestChannel {Final Channel MsgQ = New LinkedQueue(); //log<10;i ){Try{Tc.serve(); Thread.sleep(1000); }catch(InterruptedException Ie){} } } Public Void Serve() Throws InterruptedException {String Status = DoService();// returns DoService() condition puts Channel, line Cheng reads tiring-room Logger automatically extraction MsgQ.put(status); } Private String DoService() {// Do Service Here Return "service Completed OK! "; } Public TestChannel() {// Start Background Thread Runnable Logger = New Runnable() {Public Void Run() {Try {For (; ; ) System.out.println("Logger: " MsgQ.take()); } Catch (InterruptedException Ie) {} } }; New Thread(logger).start(); }}

Excutor/ThreadFactory interface: Found relevant line Cheng / reclaim / safeguard / attemper wait for the job to enclose rise, and the person that let call prep bent on is specific the encode of the task works (realize Runnable interface namely) , need not explicit found Thread kind example can asynchronous executes the mission.
Use Executor still has a profit, realize the “ lightweight ” of line Cheng to use namely. Paragraphic Ceng Di arrives in front, although we realized Runnable interface, want to found line Cheng truly, still must be finished through New Thread() , below this kind of circumstance, runnable object (the task) with Thread object (line Cheng) it is 1 pair of relations of 1. If much and the task is simple, can give every line Cheng completely to provide alignment of a task, invite Runnable boy or girl friend (the task) become N:1 with Executor objectrelation. Used Executor, we are OK above strategy of Cheng of two kinds of lines is enclosed in specific Executor implementation, come true and safeguard of convenient code.
Specific fact is existing: PooledExecutor, threadedExecutor, queuedExecutor, FJTaskRunnerGroup
Kind the relation pursues as follows:

Graph 6 Concurrent wraps Executor/ThreadFactory interface ministry to classify a relation to pursue
A paragraph of code gives out below, use PooledExecutor realizes a simple multi-line Cheng server
Code 5
Package Org.javaresearch.j2seimproved.thread;import Java.net. *;import EDU.oswego.cs.dl.util.concurrent. *;public Class TestExecutor {Public Static Void Main(String[] Args) {PooledExecutor Pool = New PooledExecutor(new BoundedBuffer(10) , 20); Pool.createThreads(4); Try {ServerSocket Socket = New ServerSocket(9999); For (; ; ) {Final Socket Connection = Socket.accept(); Pool.execute(new Runnable() {Public Void Run() {New Handler().process(connection); } }); } } Catch (Exception E) {} /
Previous 1 2 3 4 5 6 78 Next