0

状況: 関数がスレッドを呼び出して、サーバーにデータを送信します。このスレッドはさらに別のスレッドを生成し、ObjectInputStream() を使用してサーバーから結果を取得します。

最後に、このオブジェクトは、生成されたスレッドによって呼び出し元の関数に返されます。

注: スレッドは呼び出し可能であり、同期されています。

問題: 「FutureTask を MyClassName にキャストできません」という例外が発生します。

Web上でこれに対する解決策が見つかりませんでした。助言がありますか ?

クライアントコード:

public synchronized Object invoke(Object proxy, Method m, Object[] args) throws Throwable
              {

                    try {

// Lots of if-else statements
//.........

                    else if (m.toString().contains("getPosition"))
                    {                       
                        if (!offload){
                        Log.d("DProxy","Sprite Redirection takes place here 6 "+m.invoke(v, args).toString());

                        //System.out.println("PROXY Tick Argument is ");

                        return m.invoke(v, args);
                        }
                        else
                        {
                                //Code to create THREAD on the Endpoint

                        if (endpoint !=null)
                        {

                            if (!serialized)
                            {       
                                    System.out.println("serializing via proxy itself 11");
                                    this.endpoint.serialize(obj);
                                    serialized  = true;
                            }

                            Object[] args1 = new Object[args.length+1];

                            for (i=0;i<args.length;i++)
                                args1[i]=args[i];

                            args1[i]= m.toString();

 // ** Error is thrown at this line below**
     Vec2 tmp = (Vec2) this.endpoint.onClick(args1);

                                return tmp;
                                //return null; 
                            }
                            else
                                System.out.println("Endpoint is NULL");
                        }
                    }

onclick() メソッド。

public synchronized Object onClick(Object[] args) {
    try {
            latch.await();
            ctr++;
                Log.d("CLIENT","Sending Param to Client "+args[args.length-1].toString()+"  "+ctr);

        objectOutputStream.writeBoolean(false);

        // TEMP Commented
        objectOutputStream.flush();
        objectOutputStream.reset();
        objectOutputStream.writeObject(args);

        Callable<Object> worker = (Callable<Object>) new ClientThread(thisSocket,ctr);
        return executor.submit(worker);

}catch (Exception e)
{
    Log.e("ENDPOINT Exception",e.toString());
}
        Log.e("ENDPOINT","Returning blank Object");
        return new Object();
    }            

class ClientThread implements Callable <Object>{//Runnable {

    private int ctr;

    public ClientThread(Socket socket, int ctr)
    {
        this.ctr = ctr;
    }

    public synchronized Object call() {
        Vec2 res1 = null;
        Double res2=null;
        Object res = null;

        try {

            Log.v("CLIENT","Attempting to receive results from Server "+ctr);
            res = objectInputStream.readObject();

            if (res instanceof Vec2)
                {
                    res1 = (Vec2) res;
                    Log.v("CLIENT", "Object received Vec2 "+ctr);
                }
            else if (res instanceof Double)
                {
                    res2 = (Double) res;
                    Log.v("CLIENT", "Object received Double "+ctr);
                }
            else
                if(res==null)
                    Log.v("CLIENT", "Object received is NULL "+ctr);
                else
                    Log.v("CLIENT", "Object received of UNKNOWN Type  "+ctr);


    } catch (UnknownHostException e1) {
            Log.e("CLIENT receive error 1",e1.toString());
        } catch (IOException e1) {
            Log.e("CLIENT receive error 2",e1.toString());

        }
        catch (Exception e)
        {
            Log.e("CLIENT receive error 3",e.toString());
        }
        if (res1 !=null)
        return res1;
        else
            if (res2!=null)
                return res2;
            else
                return res;
    }
  //}

}
4

2 に答える 2

1

拡張/実装するクラス/インターフェースを除いて、オブジェクトをクラスまたはインターフェースにキャストすることはできません。 FutureTaskインターフェイスRunnableFutureRunnableおよび を実装しますFuture

于 2014-02-16T20:10:28.487 に答える
0

OK、今はなくなりました。ClientThread のコードを単なる関数呼び出しとして実装しました。

奇妙なのは Java のやり方です.....

于 2014-02-17T12:08:44.733 に答える