Position:Home>JAVA Development> Skill of Java process designing (semaphore, conduit)
Skill of Java process designing (semaphore, conduit)
From;  Author:Stand originally
One, semaphore

When undertaking multi-line Cheng process designing, often want to use synchronous and mutually exclusive orgnaization, but the synchronous and mutually exclusive orgnaization that Java itself did not offer, offerred two only as mutually exclusive as synchronism concerned method: Wait() and Notify() , can use design semaphore kind: MySemaphore, it is the thought design of the computation semaphore that puts forward according to Dijkstra.
MySemaphore has two the most important member methods: P() and V() . These two methods realized the P operation of semaphore and V operation actually. Specific description is as follows:
Public Synchronized Void P(){
Semaphore- - ;
If(semaphore<0){
Try{
Wait();

}catch(InterruptedException Ie){}

}

}
Public Synchronized Void V(){
Semaphore ;
If(semaphore<=0)
Notify();

}

Among them, semaphore variable recorded the condition of semaphore, wait() method is equivalent to Block primitive, use at the execution of block line Cheng, notify() method is equivalent to Wakeup primitive, use at waking up line Cheng to restore to move. Because definition of these two methods is Synchronized, fictitious machine can assure such Java the atom of these two methods is carried out, realized operation of P, V thereby.

2, conduit

The communication between the many lines Cheng of subsequent course is to use conduit to undertake normally, JDK offerred two conduit kind: PipedInpuStream and PipedOutputStream, former use at the input, latter is used at output. These two kinds of conduit should be can join for many times and shut, in realizing a process, discover they are in however after shutting, cannot establish link afresh. After the course is debugged carefully, the source code that discovers Jdk releases resource to existing when processing is shut blemish, the conduit that writes oneself because of this need kind: MyPipedInputStream and MyPipedOutputStream. These two kind accede from InputStream and OutputStream directly and come, its member method and implementation agree basically with PipedInputStream and PipedOutputStream, just shut in processing when, will kind the initiative value when the value of medium member variable restores to did not join. Additional, original conduit offerred pipeline capacity has 1024 byte only, it is when transmission data bulk is greater, may happen spill over, and the conduit in oneself kind in can install pipeline capacity arbitrarily, can be 64KB according to needing to set pipeline capacity for example. Gave out to shut exemple Cheng accordingly only below:

1. MyPipedInputStream
Public Void Close() Throws IOException {
In = -1;
Out = 0;
ClosedByReader = True;
Connected = False;
Closed = True;
Buffer = New Byte[PIPE_SIZE];

}

2. MyPipedOutputStream
Public Void Close() Throws IOException {
If (sink! = Null) {
Sink.receivedLast();
Previous12 Next