认识Java里面的Thread

  在一个特定的主线程执行的过程中,如果我们还需要在主线程的过程中插播一个线程,做其他动作。那么我们就可以利用Java的Thread类,创建一个新的线程。

  一:线程简单实现的三种方式

  (1)第一种创建线程的方式是直接extends Thread 覆盖run()方法即可。代码如下:

  

  ------------------------------------------------------------------------------------------------

  

   运行结果:这是主线程  这是线程A

  这种实现的方式的缺点是,一个java类智能extends 继承一个父类,有的时候不免有点尴尬。

    (2)第二种实现的方式是实现Runnable接口,实现run()方法。

  

  ---------------------------------------------------------------------------------

  

  运行结果:这是主线程 这是子线程B

  (3)第三种方式是 implements Callable,实现call()方法可以得到线程的执行结果;代码不在写。 

  二:什么是线程不安全

  当多个线程同时操作一个数据结构的时候产生了相互修改和串行的情况,没有保证数据的一致性,我们同城称这种代码的设计为“线程不安全的”。 

  三:什么是线程安全

  保证数据的高度一致性和准确性就叫线程安全的。

  想实现线程安全大致可以有三种方法:

  (1):多例模式,也就是不用单例模式;

  (2):使用java.util.concurrent下面的类库;

  (3):使用锁机制synchronized,lock方式;

  四:synchronized和lock锁机制

  (1):synchronized(同步锁)是java关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该代码块。

  声明如下:

  public synchronized void synMethon(){

    //方法体

  }

  public int synMethod(int a1){

    synchronized(this){

    }

  } 

  (2)Lock(显示锁)是一个接口提供了无条件的,可轮询的,定时的,可中断的锁获取操作,所有加锁和解锁的方法都是显式的。

   使用方法如下:

   class X{

     private final ReentrantLock lock = new ReentrantLock ();

     public void m(){

       lock.lock();

       try{

        方法体 

      } catch(Exception e){

         e.printStackTrace();

       }finally{

         lock.unlock()

      } 

    } 

  } 

  Lock与synchronized的比较

  a:Lock使用起来比较灵活,但必须有释放锁的动作配合;

  b: Lock必须手动开启锁和释放锁的动作,而synchronized则不需要手动释放锁和开启锁;

  c: Lock只适用与代码块加锁,而synchronized对象之间是互斥关系; 

  使用案例:

  咖狗网中运价查询行为分析:

  点击“搜索”按钮的时候,需要执行两个动作,一个是去查询,一个是行为分析;我们的主要目的是查询数据,在查询数据的同时,我们还需要进行行为分析。这个时候为了不影响功能性的操作,这个时候要启一个子线程。主线程执行查询功能,子线程去做行为分析。这两个线程互不影响。并行执行,异步执行。                                            

                                           主线程

                         ————————————》 查询功能

                                              |

                                              |

                                              |

                                       子线程(行为分析)

  比如一个接口A,B去实现这个接口,在B中:

  通过构造函数传参 

   FreightActionCollectionForElclThread thread =  new FreightActionCollectionForElclThread(0l,0l,wxid,loadportcode,dischargeportcode,null,'B');

       启动线程

     new Thread(thread).start();

 线程类C中实现业务逻辑:

            

 

 

public class FreightActionCollectionForElclThread implements Runnable {
private Logger logger = LoggerFactory
.getLogger(FreightActionCollectionForElclThread.class);
private HttpServletRequest request;
private char actiontype; // A整箱点击,B整箱查询
//微信ID
private String wxId;
//起运港
private String loadportCode;
//目的港
private String dischargeportCode;
//中转港
private String transperport_code;
//船公司
private String carrier_enname;
 
private Long productElclId;
private Long freightElclId;
//org_id,平台ID
private Long company_id;
//产品ID
private Long product_id;
 
public FreightActionCollectionForElclThread(Long productElclId,Long freightElclId,String wxId,
String loadportCode, String dischargeportCode,
HttpServletRequest request, char actiontype) {
this.productElclId = productElclId;      //通过构造函数传参
this.freightElclId = freightElclId;
this.request = request;
this.actiontype = actiontype;
this.loadportCode = loadportCode;
this.dischargeportCode = dischargeportCode;
this.wxId = wxId;
}
 
@Override
public void run() {
System.err.println("Thread -------------------------->:start!");
try {
String login_name ="";//通过wxid查询login_name
long menm_id = 0l;//通过wxid查询contract_id
PHPRPCClientService a_clientService = SpringContextHolder
.getBean("analysisPHPRPCService");
IFreightAnalysisSortService ist = a_clientService
.getPHPRPCService(IFreightAnalysisSortService.class);
 
//通过微信ID查询会员信息
PHPRPCClientService rclientService = SpringContextHolder.getBean("mmembershipPHPRPCService");
IMemberService is = rclientService.getPHPRPCService(IMemberService.class);
Member mb = is.getByWxId(wxId);
if(mb!=null){
//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓add by liuwei 20151124 微信查询行为采集,微信Id替换成手机号码或者邮箱    ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓  
String loginName = null;
Long orgId = mb.getPlatformId();
Long userId = mb.getUserId();
List<MemBinding> list = new ArrayList<MemBinding>();
IMemBinding memBind = rclientService.getPHPRPCService(IMemBinding.class);
list = memBind.getBindings(userId, orgId);
if(list != null && list.size()>0){
for(int i=0;i<list.size();i++){
Integer mode = list.get(i).getBindLoginMode();
if(mode != null && (mode.intValue() == 100 || mode.intValue() == 50)){
loginName = list.get(i).getBindLoginName();
}
}
}
login_name = loginName;
//↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑add by liuwei 20151124 微信查询行为采集,微信Id替换成手机号码或者邮箱 ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
//login_name = mb.getLoginName();
menm_id = mb.getMembId();
}
 
//通过productEfclId,freightEfclId 查询运价详情
Map<String,Object>map= new HashMap<String,Object>();
map.put("productElclId",productElclId);
map.put("freightElclId",freightElclId);
PHPRPCClientService clientService = SpringContextHolder.getBean("freightElclPHPRPCService");
IFreightElclProductService service = clientService.getPHPRPCService(IFreightElclProductService.class);
FreightElclDetailsVo freightElclDetailsVo=service.getFreightElclDetailsVo(map);
 
if(freightElclDetailsVo!=null){
company_id = freightElclDetailsVo.getOrgId();
product_id = freightElclDetailsVo.getProductElclId();
loadportCode = freightElclDetailsVo.getFreightElclProduct().getLoadport();
dischargeportCode = freightElclDetailsVo.getFreightElclProduct().getDischargeport();
transperport_code = freightElclDetailsVo.getTransferport();
carrier_enname = freightElclDetailsVo.getFreightElclProduct().getCarrierEnname();
}
 
 
 
switch (actiontype) {
case 'A':
// 拼箱产品点击行为分析
FreightPO fpo = new FreightPO();
fpo.setBi_create_time(new Date());
fpo.setBi_action_name("pm_freight_analysis_action");
fpo.setContact_id(menm_id);
fpo.setBind_login_name(login_name);
fpo.setCompany_id(company_id);
fpo.setAnalysis_freight_type("拼箱");
fpo.setProduct_id(product_id);
fpo.setLoadport_code(loadportCode);
fpo.setDischargeport_code(dischargeportCode);
fpo.setTransperport_code(transperport_code);
fpo.setCarrier_enname(carrier_enname);
fpo.setCreate_date(new Date().getTime());// 登录时间
fpo.setReside_date(0l);// 停留时间
fpo.setSource("微信");
ist.freightActionCollectForElcl(fpo, request);
break;
case 'B':
// 拼箱产品查新行为分析
FreightQueryPO queryfpo = new FreightQueryPO();
queryfpo.setBi_create_time(new Date());// 行为应用服务器时间。
queryfpo.setBi_action_name("pm_freight_query_analysis_action");
queryfpo.setContact_id(menm_id);
queryfpo.setBind_login_name(login_name);
queryfpo.setAnalysis_freight_type("拼箱");
queryfpo.setLoadport_code(loadportCode);
queryfpo.setDischargeport_code(dischargeportCode);
queryfpo.setCarrier_enname("");
queryfpo.setQuery_date(new Date().getTime());// 行为应用服务器时间。
queryfpo.setF_num(0l);// 查询出来 的数量
queryfpo.setSource("微信");
ist.freightQueryActionCollectForElcl(queryfpo, request);
break;
default:
break;
}
} catch (Exception e) {
logger.error("analysis failure", e);
 
}
System.err.println("Thread -------------------------->:end!");
}
 
}
 

 

posted @ 2015-11-16 22:32  夏日树叶  阅读(15534)  评论(0编辑  收藏  举报