程序在多播期间旋转

时间:2014-11-26 05:49:34

标签: java multithreading network-programming thread-safety multicast

以下程序最终在旋转循环中结束。我不知道可能是什么原因。如果有人能帮助我,我会非常高兴。在下面的代码中,我有一个发送器,它将一些数据多播到组224.0.1.20:64的接收者。发件人等待val类的r变量和r1类的ThreadManager来发送数据。并且在等待相同实例的val1在退出之前更改之后它会关闭。我之前尝试过join()。但它也没有用。如下所示,发送方等待有时代码正确执行输出,但有时它只是循环。

package com.test;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.util.Date;

public class Test {
    public static void main(String[] args) throws IOException, InterruptedException {
        Sender s = new Sender(64);
        ThreadManager r = new ThreadManager();
        ThreadManager r1 = new ThreadManager();
        r.start();
        r1.start();
        //Thread.currentThread().setPriority(1);
        while(r.val1 != true || r1.val1 != true){
                    >// I know this is wrong way to wait, but this is just a test
                    >// appln, so I used polling.
            //Thread.currentThread().sleep(1);
            //System.out.println(r.val1 || r1.val1);
        }
        System.out.println("going to send..");
        s.send("kaushik");
        while((r.val != true || r1.val != true) || (r.isAlive() && r1.isAlive())){
                    // same here..
            System.out.println(r.val || r.val1);
            //Thread.sleep(1000);
        }
        System.out.println("closing..");

    }
}
class ThreadManager extends Thread{
    public boolean val = false;
    public boolean val1  = false;
    @Override
    public void run() {
        // TODO Auto-generated method stub
        Reciever r;
        try {
            r = new Reciever(64);

            byte[] buf = new byte[65508];
            System.out.println("Going. to be started...");
            DatagramPacket packet = new DatagramPacket(buf, 65508);  
  val1 = true;

            r.recieve(packet);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

            val = true;
            System.out.println(val+" Id :"+Thread.currentThread().getId());


    }

}
class Sender{
    private MulticastSocket s;
    public Sender(int port) throws IOException{
        s = new MulticastSocket(port);
        s.joinGroup(InetAddress.getByName("224.0.1.20"));
    }
    public void send(String data) throws IOException{
        System.out.println("Server"+s.getLocalPort());
        DatagramPacket packet = new DatagramPacket(data.getBytes(), data.getBytes().length,InetAddress.getByName("224.0.1.20"),s.getLocalPort());
        s.send(packet);
        System.out.println("server"+s.getRemoteSocketAddress());
        s.close();
    }
}
class Reciever{
    private MulticastSocket s;
    public Reciever(int port) throws IOException{
        s = new MulticastSocket(port);
        s.joinGroup(InetAddress.getByName("224.0.1.20"));
    }
    public void recieve(DatagramPacket packet) throws IOException{
        s.receive(packet);
        byte recv[] = new byte[65508];
        System.arraycopy(packet.getData(), 0, recv, 0, packet.getData().length);
        //System.out.println(new String(recv));
        System.out.println(new String(recv).trim() + "Date : "+ new Date() + "length :" +new String(recv).trim().length());
        s.leaveGroup(InetAddress.getByName("224.0.1.20"));
        s.close();
    }
}

输出:
1。 展望。开始......
展望。开始......
要发送..
Server64
servernull
kaushikDate:11月26日星期三11:19:09 EST 2014length:7
真实ID:9
kaushikDate:11月26日星期三11:19:09 EST 2014length:7
真实ID:10

**请注意,我在ThreadManager类中将val1和val设置为true **。在上面的输出中,对于r1和r实例,val1都设置为true,但程序仍未终止。

1 个答案:

答案 0 :(得分:2)

它没有陷入僵局。 UDP是不可靠的,如果您的包丢失了,Reciever将等待数据,这看起来像死锁"。设置"超时"会更好。为接收者。

在这里你将r.val1分享给主线程,你需要" volatile"当val1值改变时,让val1立即显示给主线程,volatile也可以阻止JIT或CPU的激进优化。

public volatile boolean val1  = false;

注意在这里使用synchronized,synchronized(this)不起作用,因为这意味着synchronized(r)和synchronized(r1),那些是不同的" lock" s。如果你想保护跨线程的共享变量,请使用相同的" lock"。