import java.util.*;
public class i_othread
{
public static void main(String args[])
{
ipthread thread_1=new ipthread(); //输入线程
opthread thread_2=new opthread(); //输出线程
thread_1.start();
thread_2.start();
}
}
class ipthread extends Thread
{
public ipthread()
{
System.out.println("请输入:");
}
public void run()
{
Scanner input=new Scanner(System.in);
String name=input.nextLine();
}
}
class opthread extends Thread
{
public opthread()
{
System.out.println("输出结果为:");
}
public void run()
{
System.out.print(name);
}
}
我想要的目的很简单,就是我一边输入它一边输出,请大家帮忙修改一下程序,谢谢了
public class i_othread
{
public static void main(String args[])
{
ipthread thread_1=new ipthread(); //输入线程
opthread thread_2=new opthread(); //输出线程
thread_1.start();
thread_2.start();
}
}
class ipthread extends Thread
{
public ipthread()
{
System.out.println("请输入:");
}
public void run()
{
Scanner input=new Scanner(System.in);
String name=input.nextLine();
}
}
class opthread extends Thread
{
public opthread()
{
System.out.println("输出结果为:");
}
public void run()
{
System.out.print(name);
}
}
我想要的目的很简单,就是我一边输入它一边输出,请大家帮忙修改一下程序,谢谢了
我再改改代码,等等
private String name="";
boolean b=false;
public synchronized void put() throws InterruptedException{
if(b){
wait();
}
Scanner input = new Scanner(System.in);
System.out.println("请输入");
name = input.next();
b=true;
notify();
}
public synchronized void get() throws InterruptedException{
if(!b){
wait();
}
System.out.println("输入的是:"+name);
b=false;
notify();
}
}
class Input implements Runnable{
Q q = null;
public Input(Q q){
this.q=q;
}
public void run(){
int i=0;
while(true){
try {
q.put();
i++;
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
}
}
class Output implements Runnable{
Q q = new Q();
public Output(Q q){
this.q=q;
}
public void run(){
while(true){
try {
q.get();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class bb{
public static void main(String args[]){
Q q = new Q();
Input p = new Input(q);
Thread t = new Thread(p);
Output c = new Output(q);
Thread t1 = new Thread(c);
t.start();
t1.start();
}
}
比如那个类名为i_othread 应写成 IOThread下面的程序,并没有修改LZ写的那些类名,你可以自己修改!import java.util.*;
public class i_othread {
public static void main(String args[]) throws Exception {
Outer outer = new Outer();
Outer.ipthread thread_1=outer.new ipthread();
Outer.opthread thread_2=outer.new opthread();
}
}
class Outer {
private static String name ;
public synchronized void readInput() {
System.out.printf("\n请输入:\n");
Scanner input=new Scanner(System.in);
name=input.nextLine();
}
public synchronized void printInput() {
System.out.print("输出结果为:");
System.out.println(name);
}
public class ipthread extends Thread {
public ipthread() {
start();
}
public void run() {
try {
int i =5;
while(i-- != 0) {
readInput();
}
}catch(Exception e) {
}
}
}
public class opthread extends Thread {
public opthread() {
start();
}
public void run() {
try{
int i =5;
while(i-- != 0) {
printInput();
Thread.sleep(500);
}
} catch(Exception e) {
}
}
}
}声明:本人也是JAVA菜鸟,对于线程,更只是懂一点点点皮毛.本程序,在我的电脑上调试,可以完成LZ想要的功能
但本人并不能保证该程序是线性安全的,更不能保证程序的稳定性!
import java.io.*;public class TwoThreads {
TwoThreads Tt = new TwoThreads();
TwoThreads(){
}
static String content;
static boolean flag = false;
public static void main(String args[]) { ipthread thread_1 = new ipthread();
opthread thread_2 = new opthread();
thread_1.start();
thread_2.start();
}
static class ipthread extends Thread {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public ipthread() {
} public void run() {
while(true){
if(!flag){
try{
System.out.println("请输入:");
content = br.readLine();
flag = true;
System.out.println("hi");
// notify();
}catch(Exception ex){
ex.printStackTrace();
flag = false;
}
}
} }
}
static class opthread extends Thread {
public opthread() { } public void run() {
while(true){
if(flag){
System.out.println("your input is " + content);
flag = !flag;
}
else if(!flag){
System.out.println("I am waiting");
}
}
}
}
}
import java.io.*;
public class Receiver extends Thread {
private PipedInputStream inr = new PipedInputStream();
public PipedInputStream getInputStream()
{
return inr ;
}
public void run()
{
byte[] buf = new byte[1024];
String strInfo = new String("hello,Sender");
try{
int len = inr.read(buf);
System.out.println("the following message comes from:\n " +
new String(buf,0,len));
inr.close();
}catch(Exception e)
{
e.getStackTrace();
}
}}
import java.util.*;
import java.io.*;
public class Sender extends Thread {
private PipedOutputStream out = new PipedOutputStream();
public PipedOutputStream getOutputStream()
{
return out ;
}
public void run()
{
String strInfo = new String("hello,receiver");
try{
out.write(strInfo.getBytes());
out.close();
}catch(Exception e)
{
e.getStackTrace();
}
}
}import java.util.*;
import java.io.*;
public class ThreadTest{ /**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Receiver rec = new Receiver();
Sender sen = new Sender();
rec.start();
sen.start();
PipedInputStream in = rec.getInputStream();
PipedOutputStream out = sen.getOutputStream();
out.connect(in);
}}
public boolean canout = false;
public String str = null; public static void main(String[] args) {
practice_2 pr = new practice_2();
new out(pr).start();
new in(pr).start();
}
}
class out extends Thread {
public practice_2 pr; public out(practice_2 pr) {
this.pr = pr;
} public void run() {
while (true) {
try {
synchronized (pr) {
if (!pr.canout) {
pr.wait();
}
System.out.println(">>>" + pr.str);
pr.notifyAll();
pr.canout = false;
}
} catch (InterruptedException ex) {
ex.printStackTrace();
} }
}
}
class in extends Thread { public practice_2 pr;
BufferedReader reader = null; public in(practice_2 pr) {
this.pr = pr;
reader = new BufferedReader(new InputStreamReader(System.in));
} public void run() {
while (true) {
try {
synchronized (pr) {
if (pr.canout) {
pr.wait();
}
System.out.println("请输入:");
pr.str = reader.readLine();
pr.notifyAll();
pr.canout = true;
}
} catch (Exception ex) {
ex.printStackTrace();
} } }
}
据本人了解,输入流一旦打开就必须敲回车才能有机会关闭流,程序很难知道输入的是一个字母,还是两个字母,如果想输入一个字母输出一个字母的话,java里不知道行不行,请高人吧,据本人所知好像不行,在C里面是可以的如果非要这么干可以调用c jni本地调用
如果不敲回车,是无法捕捉到键盘输入的
但是如果真要实现LZ的想法,只能用
java.awt.event.KeyEvent 来捕捉键盘事件
不过要用图形界面的组件来捕捉
我记的nio可以做到的吧。
以前在学nio的时候 好像出现过这样的现像