启动一个线程发送消息,实现异步方法

package com.dc.thread;

import java.net.URLEncoder;

import com.dc.common.utils.MicroUtil;

/**
 * 发送语音
 * @author luhj
 *
 */
public class SendVoice implements Runnable{
	
	/**
	 * 语音服务器IP
	 */
	private String ip;
	
	/**
	 * 端口
	 */
	private int port;
	
	/**
	 * 短信内容
	 */
	private String content;
	
	/**
	 * 语音次数 
	 */
	private int times;
	
	public SendVoice(String ip,int port,String content,int times){
		this.ip = ip;
		this.port = port;
		this.content = content;
		this.times = times;
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			System.out.println(content);
			content = URLEncoder.encode(content, "UTF-8");
			String url = "http://"+ip+":"+port+"/sound"+"?content="+content;
			for(int i = 0 ; i < times; i++){
				MicroUtil.getInstance().singleVoice(url);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

 //线程调用

//发送语音
			new Thread(new SendVoice(desk.getVoiceIp(), desk.getVoicePort(), voiceContent , 2)).start();

  

posted @ 2022-03-04 16:47  红尘沙漏  阅读(99)  评论(0编辑  收藏  举报