Be in " Taming Java Threads " in the 8th chapter, I gave out the Socket that a server carries handles an order, regard a line as Cheng Chi's example. It is good about using a of the task of line Cheng pool example. Its are basic train of thought is to produce an independent boy or girl friend, its task is the Socket that a server carries monitoring. Every time when a client computer receives a server repeatedly, the Morpheus line Cheng that the object that the server carries can found beforehand from the capture in the pool, it is setting of this line Cheng the service carries join at the client. Socket server can yield a Cheng of additional client service line, but when join is shut, these additional line Cheng will be deleted. The commendation that realizes Socket server is syntactic as follows:
The tiring-room line Cheng that Socket_server object uses an independence handles the Listen() request of asynchronous, it encloses the “ of Socket to accept ” loop. When every client carries join, listen() requests a Client_handler to handle a request through calling Handle() . Every Handle() request is carried out in the line Cheng of themselves (because this is) of task of a $pooled.
Attention, every line Cheng that conveys to use themselves actually to the asynchronous message of $pooled $task will handle. Below typical situation, because a $pooled $task is used at realizing an own operation; The potential synchronous problem that concerns with visit condition variable to solving so, best means of settlement is a This is being used in $asynchronous method is the object that point to particular carbon. This that is to say, when sending an asynchronous to request to a $pooled $task, will execute operation of a Clone() , and the This finger of this method can point to this clone object. The communication between line Cheng can pass the synchronous visit of pair of Static areas to come true.
Improve Synchronized
Although be below most circumstance, $task eliminated the requirement of synchronous operation, but system of Cheng of not all multi-line comes true with the task. So, still need to improve existing line Cheng module. Synchronized key word has following drawback: Cannot assign an overtime value. Cannot interrupt to waiting to request the line Cheng of the lock. Cannot request many locks surely. (Many locks can be obtained with ordinal foreword only. (Many locks can be obtained with ordinal foreword only..
The method that solves these problems is: The grammar of patulous Synchronized, make it supports many parameter and can accept an overtime specification (the bracket middle finger below is decided) . The grammar that I hope is below:
Synchronized(x &&Y &&Z) obtains the lock of X, Y and Z object. Synchronized(x | | Y | | Z) obtains the lock of X, Y or Z object. Synchronized((x &&Y) | | Z) to in front of code a few patulous. Synchronized(. . . ) [1000] installs 1 second overtime to be locked up in order to obtain. Synchronized[1000] F(){. . . } obtains the lock of This when entering F() function, but can have 1 second overtime.
Previous 1 2 3 45 6 7 8 9 10 11 12 13 Next