package threadplus;
public class ThreadPlus extends Thread{
protected Queue que;
/**
* @param aFileName full name of an existing, readable file.
*/
public ThreadPlus(int queSize)
{
que=new Queue(queSize);
}
public void run()
{
int msgSize = 0;
int bufSize = que.getQueSize();
byte[] buf = new byte[bufSize];
init();
while (true)
{
msgSize=0;
msgSize=que.getMessage(buf, bufSize, 0);
if(msgSize<0)
continue;
onGetMsg(buf,msgSize);
}
}
public int putMessage(byte[] msg, int msgSize)
{
int ret;
ret=que.putMessage(msg, msgSize);
return ret;
}
public void onGetMsg(byte[] msg, int msgSize)
{
if(msgSize>0)
{
System.out.println("thread plus got a message!");
}
}
public void init()
{
System.out.println("thread plus started!");
}
}
本文介绍了一个名为ThreadPlus的自定义线程类的实现细节。该类继承自标准Thread类,并添加了消息队列的功能,能够接收消息并进行处理。主要方法包括putMessage用于发送消息,onGetMsg用于处理接收到的消息。
---线程的实现&spm=1001.2101.3001.5002&articleId=2770276&d=1&t=3&u=0247e789d30848cb9cbe28f1b16ac9e0)
1718

被折叠的 条评论
为什么被折叠?



