现在的位置: 首页 > 综合 > 正文

通过Socket实现进程间异步通讯(四)

2013年11月28日 ⁄ 综合 ⁄ 共 1002字 ⁄ 字号 评论关闭

第四步:做一个比较复杂一点的进程间通讯

现在需要通讯的双方均为线程,且处于不同的进程之间

 

package com.hode.thread;

/**
 * @author 泰伯子仪
 *
 * Test3是一个线程,处于独立的进程中
 * Test3希望通过socket与另外一个进程中的线程通讯
 * 其中Test3与socket的客户端邦定
 * 而通讯的另一方则是与socket的服务端邦定
 *
 */

public class Test3 extends CommThread implements DealWith
{
    private static Test3 test3 = new Test3(2000);
    private SignalSocketThread ssThread = new SignalSocketThread();
   
    public Test3(int sleepTime)
    {
        super(sleepTime);
        this.setName("Test3Thread");
    }
   
    /**
     * Test3必须继承CommThread
     * 覆写dispose()
     * dispose()为线程中需要处理的事情
     */
    public void dispose()
    {
        //test3和Socket信号的客户端邦定
        ssThread.ClientBind(test3); 
    }
   
    /**
     * Test3必须继承接口DealWith
     * 实现函数dealWith()
     */
    public void dealwith()
    {
        for(int i=0 ; i<10 ; i++)
        {
            try
            {
                Thread.sleep(500);
            }
            catch (InterruptedException e){}
            System.out.print(i+" ");
        }
    }

    public static void main(String[] args)
    {  
        test3.threadStart();    
    }
}

 

好了,现在编译此类,运行Test1和Test3看看有什么效果

抱歉!评论已关闭.