All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.weicoder.common.thread.ThreadUtil Maven / Gradle / Ivy

package com.weicoder.common.thread;

import java.util.Map;

import com.weicoder.common.constants.C;
import com.weicoder.common.lang.W.M;
import com.weicoder.common.log.Logs; 

/**
 * Thread 相关方法
 * 
 * @author WD
 */
public sealed class ThreadUtil permits T {
	// 保存线程属性
	private final static Map> ATTR = M.map();

	/**
	 * 为当前线程保存属性
	 * 
	 * @param key 键
	 * @param val 值
	 */
	public static Object put(String key, Object val) {
		return M.getMap(ATTR, current()).put(key, val);
	}

	/**
	 * 获得当前线程的值
	 * 
	 * @param key 键
	 * @return 值
	 */
	public static Object get(String key) {
		return M.getMap(ATTR, current()).get(key);
	}

	/**
	 * 获得当前线程
	 */
	public static Thread current() {
		return Thread.currentThread();
	}

	/**
	 * 封装sleep异常处理
	 * 
	 * @param millis 暂停时间毫秒
	 */
	public static void sleep(long millis) {
		try {
			Thread.sleep(millis);
		} catch (InterruptedException e) {
			Logs.error(e);
		}
	}

	/**
	 * 封装sleep异常处理
	 * 
	 * @param millis 暂停时间毫秒
	 */
	public static void sleep(int millis) {
		sleep(millis * C.D.TIME_SECOND);
	}

	/**
	 * 执行线程
	 * 
	 * @param target 线程接口
	 */
	public static void start(Runnable target) {
		new Thread(target).start();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy