Communication between goroutine communication and thread in Java

Source: Internet
Author: User

This file contains the Implementation of Go Channels.

invariants:

//  At least one of c.sendq and c.recvq is empty,

//  Except for the case of an unbuffered channel with< span class= "s2" > a single Goroutine

//  Blocked on it for both sending and receiving using a select statement,

//  In which case the length of c.sendq and c.recvq is limited only by The

Size of the Select statement.

//

for buffered channels, also:

//  C.qcount > 0 implies that c.recvq is empty.

c.qcount < C.dataqsiz implies that c.sendq is empty.

Import (

"Runtime/internal/atomic"

"unsafe"

)

    • the data structure in the channel is Hchan{}

Chan How to initialize:

func Makechan (t *chantype, size int) *hchan

Memory allocation Method:

With no buffers

C = (*hchan) (MALLOCGC (hchansize, nil, true))

with buffers

C = (*hchan) (MALLOCGC (hchansize+uintptr(size) *elem.size, nil, true))

—————-

Lock (&c.lock)

if c.closed! = 0 {

unlock (&c.lock)

Panic(Plainerror ("Send on closed Channel"))

}

——————

Because there are locking steps in the Chansend, It is safe to read and write multiple goroutine.

The PipedInputStream class and the PipedOutputStream class are used to create pipeline traffic in the application.

A PipedInputStream instance object must be connected to a PipedOutputStream instance object to produce a communication pipeline.

( must use connect,in.connect (out) or out.connect (in), the effect is equivalent )

PipedOutputStream can write data to the pipeline,pipedintputstream can read pipedoutputstream data written to the pipeline, These two classes are primarily used to complete communication between threads.

The data is read from within the [Out,in] range and written to the [In,out] range

{----X x x x x x X----}

^             ^

|             |

Out in

{x x x x--------X x x}

^               ^

|               |

In out

In pipedintputstream , the default size of the buffer is:

private static final int default_pipe_size = 1024x768;

When writing data:

synchronized void receive (byte b[], int off, int len) throws ioexception

byte[], and offsets are calculated

Communication between goroutine communication and thread in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.