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

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

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

 

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看看有什么效果

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值