while (true) {
System.out.println("ClIENT***********************");
Socket client = new Socket("10.33.0.9", 2220);
File f = null;
synchronized (mutex.obj) {// 对输入管道同步
System.out.println("CLIENTSYNCHRONIZED");
if (mutex.flag) {//对管道数据进行判断 有数据
InputStream in = client.getInputStream();
int b = 0;
String str = "";
while ((b = in.read()) != -1) {
str += (char) b;
}
f = new File(str);
f.createNewFile();
System.out.println("文件创建成功");
client.close();
mutex.flag = false;
System.out.println("client wait");
mutex.obj.notify();//唤醒server  我想再这里阻塞自己怎么办啊 ,要是用wait那又不能释放同步锁,就发生死锁啦 
}
else
mutex.obj.wait();
}

解决方案 »

  1.   

    想救你啊,什么wait不是释放同步锁,wait会释放锁啊。
      

  2.   

    import java.io.*;
    import java.net.*;
    import java.util.*;public class ThreadTcp {
    /**
     * @param args
     */
    public static void main(String[] args) {
    Thread SERVERthread = new Thread(new TCPServerThread());
    SERVERthread.start();
    Thread CLIENTthread1 = new Thread(new TCPClientThread());
    // Thread CLIENTthread2 = new Thread(new TCPClientThread());
    // Thread CLIENTthread3 = new Thread(new TCPClientThread());
    CLIENTthread1.start();
    // CLIENTthread2.start();
    // CLIENTthread3.start(); }
    }class mutex {
    public static Object obj = new Object();
    // public static Object obj1 = new Object();
    public static boolean flag = false;
    }class TCPServerThread implements Runnable {
    public void run() { try { while (true) { System.out.println("SERVER*****************************");
    ServerSocket server = new ServerSocket(2220);
    File f = new File("D:/JavaWorkspace/day12/src/TcpServer.java");
    synchronized (mutex.obj) {// 同步代码块对输出管道同步
    System.out.println("SERVERsynchronized"); while(mutex.flag) // 有数据就放入数据
    mutex.obj.wait();

    System.out.println("server flag");
    Socket socket = server.accept();
    DataOutputStream dos = new DataOutputStream(
    socket.getOutputStream());
    System.out.println("请输入要传递的文件名和将要存放文件的详细地址");
    Scanner in = new Scanner(System.in);
    String path = in.next();// 文件名
    if (path != null) {
    dos.writeBytes(path);
    } dos.flush();
    dos.close();
    socket.close();
    mutex.flag = true;// /////////////////////
    mutex.obj.notify();// 唤醒client
    System.out.println("server notify");
    }
    // /////////////////////
    Socket socket1 = server.accept();
    synchronized (mutex.obj) {
    System.out.println("wenjian chuanshu SERVER");
    while(mutex.flag) // 有数据
    mutex.obj.wait();
    DataOutputStream dos1 = new DataOutputStream(
    socket1.getOutputStream()); FileInputStream fr = new FileInputStream(f);
    int b = 0;
    while ((b = fr.read()) != -1) {
    dos1.write(b);
    }
    dos1.flush();
    dos1.close();
    server.close();
    socket1.close();
    mutex.flag = true;// 放入了数据,将判断条件改变
    System.out.println("server over");
    mutex.obj.notify();// 唤醒client
    System.out.println("server notify");
    }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }class TCPClientThread implements Runnable {
    public void run() {// 对CLIENT进程同步
    try {
    while (true) {
    System.out.println("ClIENT***********************");
    Socket client = new Socket("10.33.0.9", 2220);
    File f = null;
    synchronized (mutex.obj) {// 对输入管道同步
    System.out.println("CLIENTSYNCHRONIZED");
    while (!mutex.flag)// 对管道数据进行判断 有数据
    mutex.obj.wait(); InputStream in = client.getInputStream();
    int b = 0;
    String str = "";
    while ((b = in.read()) != -1) {
    str += (char) b;
    }
    f = new File(str);
    f.createNewFile();
    System.out.println("文件创建成功");
    client.close();
    mutex.flag = false;
    System.out.println("client wait");
    mutex.obj.notify();// 唤醒server  怎么阻塞自身呢?????
    }
    Thread.currentThread().interrupt();
    // //////////////////////////////////////
    Socket client1 = new Socket("10.33.0.9", 2220);
    System.out.println("clieng why??????????");
    synchronized (mutex.obj) {
    while (mutex.flag)
    mutex.obj.wait(); System.out.println("wenjian chuanshu CLIENT");
    int bc = 0;
    BufferedInputStream buffin = new BufferedInputStream(
    client1.getInputStream());// 将管道中的数据读入缓冲区
    System.out.println("wenjian chuanshu CLIENT*******");
    FileOutputStream fw = new FileOutputStream(f);
    while ((bc = buffin.read()) != -1) {
    fw.write(bc);
    }
    System.out.println("文件传输成功");
    fw.close();
    client1.close();
    buffin.close();
    System.out.println("client over");
    mutex.flag = false;// //////////////////////////////
    System.out.println("client wait");
    mutex.obj.notify();// 唤醒server  }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
      

  3.   

    这个题目是用多线程实现tcp传输文件Client端和Server端共享一个锁mutex.obj
    第一次是传文件名 第二次是传文件内容
    现在Client取了数据 唤醒Server 可是怎么阻塞自身呢,让Server将文件内容传过来啊 
      

  4.   

    没搞明白你为什么要在那里阻塞,要的是需要满足什么样条件才能解除的阻塞。你要的是等待、睡眠、挂起还是阻塞。不过你可以把mutex.obj.wait();前面的else去掉,不知道是不你想要的
      

  5.   

    ???没有else啊  哪有这句啊 
      

  6.   

    如果你是想让server和client互斥,可以再加一个同步对象。
      

  7.   

    我想过加一个同步对象了 ,可是 client不阻塞的话,当client没有进入第二个同步代码块的时候,client就会不执行第二次同步代码块,无法执行写文件操作啊