线程println并读取,使println具有优先级 [英] thread println and read so that println has priority

查看:185
本文介绍了线程println并读取,使println具有优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

适应Apache WeatherTelnet 代码,我正在致力于 MUD客户端,但正在努力与并发输入输出。

Adapting Apache WeatherTelnet code, I'm working towards a MUD client, but am struggling with concurrent input/output. It's not possible to read and write simultaneously, of course.

对于此应用程序, out 应优先于中。如果没有写入 java.lang.System.out ,那么,只有这样,应该输入 java.lang.System.in 被读取。我不知道该如何完成,因为 out 交织在一起。

For this application, out should have priority over in. If nothing is being written to java.lang.System.out then, and only then, should input from java.lang.System.in be read. I'm just not sure how to accomplish that because in and out are interwoven.

运行和内存转储:

thufir@dur:~$ 
thufir@dur:~$ 
thufir@dur:~$ java -jar NetBeansProjects/WeatherTelnet/dist/WeatherTelnet.jar 
------------------------------------------------------------------------------
*               Welcome to THE WEATHER UNDERGROUND telnet service!            *
------------------------------------------------------------------------------
*                                                                            *
*   National Weather Service information provided by Alden Electronics, Inc. *
*    and updated each minute as reports come in over our data feed.          *
*                                                                            *
*   **Note: If you cannot get past this opening screen, you must use a       *
*   different version of the "telnet" program--some of the ones for IBM      *
*   compatible PC's have a bug that prevents proper connection.              *
*                                                                            *
*           comments: jmasters@wunderground.com                              *
------------------------------------------------------------------------------

Press Return to continue:
fgf
d
e
f





2013-08-30 14:13:49
Full thread dump Java HotSpot(TM) Client VM (23.25-b01 mixed mode):

"Thread-2" daemon prio=10 tid=0xa0595400 nid=0x1c60 runnable [0x9fcdf000]
   java.lang.Thread.State: RUNNABLE
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(SocketInputStream.java:150)
    at java.net.SocketInputStream.read(SocketInputStream.java:121)
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:273)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:334)
    - locked <0xa0af5740> (a java.io.BufferedInputStream)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:254)
    - locked <0xa0afa1e0> (a org.apache.commons.net.telnet.TelnetInputStream)
    at org.apache.commons.net.telnet.TelnetInputStream.__read(TelnetInputStream.java:141)
    at org.apache.commons.net.telnet.TelnetInputStream.run(TelnetInputStream.java:611)
    at java.lang.Thread.run(Thread.java:724)

"DestroyJavaVM" prio=10 tid=0xb6b06400 nid=0x1c56 waiting on condition [0x00000000]
   java.lang.Thread.State: RUNNABLE

"Thread-1" prio=10 tid=0x9fd20400 nid=0x1c5f in Object.wait() [0x9fe79000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0xa0afca50> (a [I)
    at java.lang.Object.wait(Object.java:503)
    at org.apache.commons.net.telnet.TelnetInputStream.read(TelnetInputStream.java:392)
    - locked <0xa0afca50> (a [I)
    at org.apache.commons.net.telnet.TelnetInputStream.read(TelnetInputStream.java:534)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:254)
    - locked <0xa0afebc8> (a java.io.BufferedInputStream)
    at weathertelnet.WeatherTelnet.consoleOutput(WeatherTelnet.java:65)
    at weathertelnet.WeatherTelnet$2.run(WeatherTelnet.java:33)
    at java.lang.Thread.run(Thread.java:724)

"Thread-0" prio=10 tid=0x9fd1f000 nid=0x1c5e runnable [0xa045c000]
   java.lang.Thread.State: RUNNABLE
    at java.io.FileInputStream.readBytes(Native Method)
    at java.io.FileInputStream.read(FileInputStream.java:242)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:254)
    - locked <0xa0a16d88> (a java.io.BufferedInputStream)
    at weathertelnet.WeatherTelnet.consoleInput(WeatherTelnet.java:47)
    at weathertelnet.WeatherTelnet$1.run(WeatherTelnet.java:22)
    at java.lang.Thread.run(Thread.java:724)

"Service Thread" daemon prio=10 tid=0xb6bc3400 nid=0x1c5c runnable [0x00000000]
   java.lang.Thread.State: RUNNABLE

"C1 CompilerThread0" daemon prio=10 tid=0xb6bc1400 nid=0x1c5b waiting on condition [0x00000000]
   java.lang.Thread.State: RUNNABLE

"Signal Dispatcher" daemon prio=10 tid=0xb6bbfc00 nid=0x1c5a waiting on condition [0x00000000]
   java.lang.Thread.State: RUNNABLE

"Finalizer" daemon prio=10 tid=0xb6b83c00 nid=0x1c59 in Object.wait() [0xa04fe000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0xa0a05698> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:135)
    - locked <0xa0a05698> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:151)
    at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:189)

"Reference Handler" daemon prio=10 tid=0xb6b82000 nid=0x1c58 in Object.wait() [0xa065d000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0xa0a05270> (a java.lang.ref.Reference$Lock)
    at java.lang.Object.wait(Object.java:503)
    at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:133)
    - locked <0xa0a05270> (a java.lang.ref.Reference$Lock)

"VM Thread" prio=10 tid=0xb6b7c400 nid=0x1c57 runnable 

"VM Periodic Task Thread" prio=10 tid=0xb6bcd800 nid=0x1c5d waiting on condition 

JNI global references: 171

Heap
 def new generation   total 4928K, used 1299K [0xa0a00000, 0xa0f50000, 0xa5f50000)
  eden space 4416K,  29% used [0xa0a00000, 0xa0b44d90, 0xa0e50000)
  from space 512K,   0% used [0xa0e50000, 0xa0e50000, 0xa0ed0000)
  to   space 512K,   0% used [0xa0ed0000, 0xa0ed0000, 0xa0f50000)
 tenured generation   total 10944K, used 0K [0xa5f50000, 0xa6a00000, 0xb0a00000)
   the space 10944K,   0% used [0xa5f50000, 0xa5f50000, 0xa5f50200, 0xa6a00000)
 compacting perm gen  total 12288K, used 2060K [0xb0a00000, 0xb1600000, 0xb4a00000)
   the space 12288K,  16% used [0xb0a00000, 0xb0c030d0, 0xb0c03200, 0xb1600000)
No shared spaces configured.




^Cthufir@dur:~$ 
thufir@dur:~$ 
thufir@dur:~$ 
thufir@dur:~$ 

代码:

package weathertelnet;

import static java.lang.System.in;
import static java.lang.System.out;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.SocketException;
import org.apache.commons.net.telnet.TelnetClient;

public final class WeatherTelnet implements Runnable {

    @Override
    public void run() {


        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    consoleInput();
                } catch (IOException ex) {
                }
            }
        }).start();

        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    consoleOutput();
                } catch (SocketException ex) {
                } catch (IOException ex) {
                }
            }
        }).start();
    }

    public void consoleInput() throws IOException {
        do {
            char ch = (char) in.read();
            StringBuilder s = new StringBuilder();
            s.append(ch);
            while (255 > ch && ch >= 0) {
                ch = (char) in.read();
                s.append(ch);
            }
            out.println("\tyour text\t"+ s);
        } while (true);        
    }

    public void consoleOutput() throws SocketException, IOException {
        TelnetClient tc;
        tc = new TelnetClient();
        tc.connect("rainmaker.wunderground.com", 3000);

        InputStream inputStream = tc.getInputStream();

        char ch = (char) inputStream.read();

        while (255 > ch && ch >= 0) {
            out.print(ch);
            ch = (char) inputStream.read();
        }
    }

    public WeatherTelnet() {
        run();
    }

    public static void main(String[] args) {
        new WeatherTelnet();
    }
}

*代码在ASL下,请: p>

*code is under the ASL, please:

推荐答案

正确的处理方式是将它移交给实用程序类,只需导入示例。 util.IOUtil 来自Apache:

The correct way to handle this is to hand it over to the utility class, and just import examples.util.IOUtil from Apache:

package weathertelnet;

import examples.util.IOUtil;
import java.io.IOException;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Properties;
import org.apache.commons.net.telnet.TelnetClient;

public class WeatherTelnet {

    private TelnetClient telnetConnection = new TelnetClient();

    public WeatherTelnet() throws SocketException, IOException {
        System.out.println("weather telnet..");
        Properties props = PropertiesReader.getProps();
        InetAddress host = InetAddress.getByName(props.getProperty("host"));
        int port = Integer.parseInt(props.getProperty("port"));
        telnetConnection.connect(host, port);
        System.out.println("connected");
        IOUtil.readWrite(telnetConnection.getInputStream(), telnetConnection.getOutputStream(), System.in, System.out);
    }

    public static void main(String[] args) throws SocketException, IOException {
        System.out.println("main...");
        new WeatherTelnet();
    }
}

这篇关于线程println并读取,使println具有优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆