简体   繁体   中英

Any keyword or methods in Java similar to Go's Select?

Java中是否有任何类似于Go的select语句的关键字或方法,仅用于选择通信通道?

The other answers misunderstand the question (or they don't know what select is in Go).

Go does have a switch and a select statement, where the switch is the rough equivalent of Java's switch , and the select statement is similar to switch but only to select on communication operations (the cases are comm. ops.: a send statement or a receive operation).

Java does not have an equivalent to Go's select . You can achieve the same but in a more verbose way, and it's not built into the language like Go's select .

See related questions:

Concurrency Java example of Go

Equivalent of golang channel in Java

Go channel vs Java BlockingQueue

The similar syntax in Java is switch . For detail, you could refer to https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html .

switch (month) {
    case 1:  monthString = "January";
             break;
    case 2:  monthString = "February";
             break;
    default: monthString = "Invalid month";
             break;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM