RocketMQ有很多的线程服务,这些服务都继承自抽象类ServiceThread。

这个抽象类可以单独抽出来用到我们其他的项目中来,仅仅需要修改下日志模块:

/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ /**
* @author shijia.wxr
*/
public abstract class ServiceThread implements Runnable {
// 抽象类可以不用实现接口中的方法,但是继承改抽象类的类就必须实现了。
private static final long JoinTime = 90 * 1000; protected final Thread thread; protected volatile boolean hasNotified = false; protected volatile boolean stoped = false; public ServiceThread() {
this.thread = new Thread(this, this.getServiceName());
} public abstract String getServiceName(); public void start() {
this.thread.start();
} public void shutdown() {
this.shutdown(false);
} public void shutdown(final boolean interrupt) {
this.stoped = true;
System.out.println();
System.out.println("shutdown thread " + this.getServiceName() + " interrupt " + interrupt);
synchronized (this) {
if (!this.hasNotified) {
this.hasNotified = true;
this.notify();
}
} try {
if (interrupt) {
this.thread.interrupt();
} long beginTime = System.currentTimeMillis();
if (!this.thread.isDaemon()) {
this.thread.join(this.getJointime());
}
long eclipseTime = System.currentTimeMillis() - beginTime;
System.out.println("join thread " + this.getServiceName() + " eclipse time(ms) " + eclipseTime + " "
+ this.getJointime());
} catch (InterruptedException e) {
e.printStackTrace();
}
} public long getJointime() {
return JoinTime;
} public void stop() {
this.stop(false);
} public void stop(final boolean interrupt) {
this.stoped = true;
System.out.println("stop thread " + this.getServiceName() + " interrupt " + interrupt);
synchronized (this) {
if (!this.hasNotified) {
this.hasNotified = true;
this.notify();
}
} if (interrupt) {
this.thread.interrupt();
}
} public void makeStop() {
this.stoped = true;
System.out.println("makestop thread " + this.getServiceName());
} public void wakeup() {
synchronized (this) {
if (!this.hasNotified) {
this.hasNotified = true;
this.notify();
}
}
} protected void waitForRunning(long interval) {
synchronized (this) {
if (this.hasNotified) {
this.hasNotified = false;
this.onWaitEnd();
return;
} try {
this.wait(interval);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
this.hasNotified = false;
this.onWaitEnd();
}
}
} protected void onWaitEnd() {
} public boolean isStoped() {
return stoped;
}
}

使用方法:

继承这个类,需要实现两个方法,一个来自runnable接口的run方法,一个是来自ServiceThread 的getServiceName方法。

getServiceName方法主要是未来用来初始化线程。在上面标黄的位置可以看到。

构造完对象之后调用抽象父类ServiceThread的start方法就能启动线程了(上面标黄红字部分)。

你还可以通过这篇文章来观察一下,在rocketmq中一个完整的使用流程是怎样的:

http://www.cnblogs.com/guazi/p/6850988.html



rocketmq的线程服务基类的更多相关文章

  1. 请高手解释这个C#程序,其中ServiceBase是windows服务基类,SmsService是

    请高手解释这个C#程序,其中ServiceBase是windows服务基类,SmsService是 ServiceBase的子类. static void Main() { ServiceBase[] ...

  2. ASP.NET MVC 学习笔记-2.Razor语法 ASP.NET MVC 学习笔记-1.ASP.NET MVC 基础 反射的具体应用 策略模式的具体应用 责任链模式的具体应用 ServiceStack.Redis订阅发布服务的调用 C#读取XML文件的基类实现

    ASP.NET MVC 学习笔记-2.Razor语法   1.         表达式 表达式必须跟在“@”符号之后, 2.         代码块 代码块必须位于“@{}”中,并且每行代码必须以“: ...

  3. 工作线程基类TaskSvc

    工作线程基类TaskSvc 前端时间用ACE写代码,发ACE_Task确实好用.不但能提供数量一定的线程,还能够让这些继承的线程函数自由访问子类的private和protected变量.此外,ACE_ ...

  4. C#WinForm线程基类

    在CS模式开发中一般我们需要用到大量的线程来处理比较耗时的操作,以防止界面假死带来不好的体验效果,下面我将我定义的线程基类给大家参考下,如有问题欢迎指正. 基类代码 #region 方法有返回值 // ...

  5. RTTI、虚函数和虚基类的实现方式、开销分析及使用指导(虚函数的开销很小,就2次操作而已)

    白杨 http://baiy.cn “在正确的场合使用恰当的特性” 对称职的C++程序员来说是一个基本标准.想要做到这点,首先要了解语言中每个特性的实现方式及其开销.本文主要讨论相对于传统 C 而言, ...

  6. 【C# 线程】WaitHandle类

    理论 Windows的线程同步方式可分为2种,用户模式构造和内核模式构造.内核模式构造:是由Windows系统本身使用,内核对象进行调度协助的.内核对象是系统地址空间中的一个内存块,由系统创建维护. ...

  7. 基于SqlSugar的开发框架循序渐进介绍(6)-- 在基类接口中注入用户身份信息接口

    在基于SqlSugar的开发框架中,我们设计了一些系统服务层的基类,在基类中会有很多涉及到相关的数据处理操作的,如果需要跟踪具体是那个用户进行操作的,那么就需要获得当前用户的身份信息,包括在Web A ...

  8. 基于SqlSugar的开发框架循序渐进介绍(10)-- 利用axios组件的封装,实现对后端API数据的访问和基类的统一封装处理

    在SqlSugar的开发框架的后端,我们基于Web API的封装了统一的返回结果,使得WebAPI的接口返回值更加简洁,而在前端,我们也需要统一对返回的结果进行解析,并获取和Web API接口对应的数 ...

  9. Entity Framework 实体框架的形成之旅--基类接口的统一和异步操作的实现(3)

    在本系列的第一篇随笔<Entity Framework 实体框架的形成之旅--基于泛型的仓储模式的实体框架(1)>中介绍了Entity Framework 实体框架的一些基础知识,以及构建 ...

随机推荐

  1. poj2104 主席树 区间K大 在线 无修改

    关于主席树: 主席树(Chairman Tree)是一种离线数据结构,使用函数式线段树维护每一时刻离散之后的数字出现的次数,由于各历史版本的线段树结构一致,可以相减得出区间信息,即该区间内出现的数字和 ...

  2. AC日记——「HNOI2017」单旋 LiBreOJ 2018

    #2018. 「HNOI2017」单旋 思路: set+线段树: 代码: #include <bits/stdc++.h> using namespace std; #define max ...

  3. AC日记——旅游 bzoj 2157

    2157 思路: LCT: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 400005 #define IN ...

  4. 归并排序(MergeSort)

    原帖:http://blog.csdn.net/magicharvey/article/details/10192933 算法描述 归并排序(MergeSort)是采用分治法的一个非常典型的应用.通过 ...

  5. pytho中pickle、json模块

    pickle & json 模块 json,用于字符串 和 python数据类型间进行转换 pickle,用于python特有的类型 和 python的数据类型间进行转换 json模块提供了四 ...

  6. hihoCoder #1870 : Jin Yong’s Wukong Ranking List-闭包传递(递归) (ACM-ICPC Asia Beijing Regional Contest 2018 Reproduction A) 2018 ICPC 北京区域赛现场赛A

    P1 : Jin Yong’s Wukong Ranking List Time Limit:1000ms Case Time Limit:1000ms Memory Limit:512MB Desc ...

  7. textbox获取当前光标位置,在光标后面插入字符串

    左侧输入要复制的字符串,点击复制btn,将左侧字符串复制到后侧, 示例: 输入框                复制后 12345 12345 (光标落在3后) aaa 123aaa45(光标落在4后 ...

  8. SSH整合错误三连

    访问Action错误 ognl.MethodFailedException: Method "add" failed for object com.test3.action.Use ...

  9. 洛谷——P1869 愚蠢的组合数

    P1869 愚蠢的组合数 题目描述 最近老师教了狗狗怎么算组合数,狗狗又想到了一个问题... 狗狗定义C(N,K)表示从N个元素中不重复地选取K个元素的方案数. 狗狗想知道的是C(N,K)的奇偶性. ...

  10. poj 1050(矩阵求和问题dp)

    To the Max Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44765   Accepted: 23700 Desc ...