45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:怎么样通过Socket实现进程间异步通讯?

怎么样通过Socket实现进程间异步通讯?

2016-08-29 16:03:55 来源:www.45fan.com 【

怎么样通过Socket实现进程间异步通讯?

最近写进程间通讯有所心得,故而在此记录以备后忘,也希望大家多提建议

第一步写一个线程类 CommThread.java

package com.hode.thread;

/**
* @author泰伯子仪
*/
public abstract class CommThread extends Thread
{

private boolean shouldRun = false;

private int sleepTime = 2000;

private boolean isWait = false;

private boolean isWork = true;

/**
*
*/
public CommThread()
{
//super("CommThread");
Thread commThread = new Thread(CommThread.this);
shouldRun = true;
}

public CommThread(int sleepTime)
{
//super("CommThread");
Thread commThread = new Thread(CommThread.this);
shouldRun = true;
this.sleepTime = sleepTime;
}

public void threadStart()
{
start();
}

public void run()
{
while (shouldRun)
{
try
{
sleep(sleepTime);
}
catch (InterruptedException e)
{
System.err.println("线程Thread1意外终止。");
}
if (!getWait())
{
isWork = true;
System.out.println("/n处理开始");

dispose();

isWork = false;
System.out.println("/n处理完毕");
}
else
{
isWork = false;
System.out.println("等待中.../n");
}
}
}

public void shutdown()
{
shouldRun = false;
}

public void setWait(boolean bool)
{
isWait = bool;
}

public boolean getWait()
{
return isWait;
}

public boolean getWork()
{
return isWork;
}

public abstract void dispose();

}

该类为虚类,其中需函数public abstract void dispose()是线程中需要处理的程序部分

 

本文地址:http://www.45fan.com/a/question/69387.html
Tags: 实现 通过 Socket
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部