java中线程池实现
方式一
package com.toltech.springboot.test;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @author Wgs
* @version 1.0
* @create:2017/11/27
*/
class MyRunnables implements Runnable {
@Override
public void run() {
for (int x = 0; x < 100; x++) {
System.out.println(Thread.currentThread().getName() + ":" + x);
}
}
}
/*
* 线程池的好处:线程池里的每一个线程代码结束后,并不会死亡,而是再次回到线程池中成为空闲状态,等待下一个对象来使用。
*
* 如何实现线程的代码呢?
* A:创建一个线程池对象,控制要创建几个线程对象。
* public static ExecutorService newFixedThreadPool(int nThreads)
* B:这种线程池的线程可以执行:
* 可以执行Runnable对象或者Callable对象代表的线程
* 做一个类实现Runnable接口。
* C:调用如下方法即可
* Future<?> submit(Runnable task)
* <T> Future<T> submit(Callable<T> task)
* D:我就要结束,可以吗?
* 可以。
*/
public class ExecutorsDemo {
public static void main(String[] args) {
// 创建一个线程池对象,控制要创建几个线程对象。
// public static ExecutorService newFixedThreadPool(int nThreads)
ExecutorService pool = Executors.newFixedThreadPool(2);
// 可以执行Runnable对象或者Callable对象代表的线程
pool.submit(new MyRunnables());
pool.submit(new MyRunnables());
//结束线程池
pool.shutdown();
}
}
方式二
package com.toltech.springboot.test;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @author Wgs
* @version 1.0
* @create:2017/11/27
*/
/*
* 多线程实现的方式3:
* A:创建一个线程池对象,控制要创建几个线程对象。
* public static ExecutorService newFixedThreadPool(int nThreads)
* B:这种线程池的线程可以执行:
* 可以执行Runnable对象或者Callable对象代表的线程
* 做一个类实现Runnable接口。
* C:调用如下方法即可
* Future<?> submit(Runnable task)
* <T> Future<T> submit(Callable<T> task)
* D:我就要结束,可以吗?
* 可以。
*/
public class CallableDemo {
public static void main(String[] args) {
//创建线程池对象
ExecutorService pool = Executors.newFixedThreadPool(2);
//可以执行Runnable对象或者Callable对象代表的线程
pool.submit(new MyCallable());
pool.submit(new MyCallable());
//结束
pool.shutdown();
}
}
//Callable:是带泛型的接口。
//这里指定的泛型其实是call()方法的返回值类型。
class MyCallable implements Callable {
@Override
public Object call() throws Exception {
for (int x = 0; x < 100; x++) {
System.out.println(Thread.currentThread().getName() + ":" + x);
}
return null;
}
}
Spring
ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--自动扫描-->
<context:component-scan base-package="com.wanggs">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--数据库连接池-->
<context:property-placeholder location="classpath:config.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--基于注解的事务-->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!--MyBatis-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.wanggs.pojo"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
<property name="configuration">
<bean class="org.apache.ibatis.session.Configuration">
<property name="mapUnderscoreToCamelCase" value="true"/>
</bean>
</property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.wanggs.mapper"/>
</bean>
<!--邮件处理-->
<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${mail.host}"/>
<property name="username" value="${mail.username}"/>
<property name="password" value="${mail.password}"/>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
</props>
</property>
</bean>
<!--线程池-->
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="10"/>
<property name="maxPoolSize" value="20"/>
<property name="queueCapacity" value="20"/>
</bean>
<!--redis连接池的配置-->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.pool.maxActive}"/>
<property name="maxIdle" value="${redis.pool.maxIdle}"/>
<property name="minIdle" value="${redis.pool.minIdle}"/>
<property name="maxWaitMillis" value="${redis.pool.maxWait}"/>
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/>
<property name="testOnReturn" value="${redis.pool.testOnReturn}"/>
</bean>
<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
<constructor-arg index="0" ref="jedisPoolConfig"/>
<constructor-arg index="1" value="${redis.host}" />
<constructor-arg index="2" value="${redis.port}" />
<constructor-arg index="3" value="${redis.timeout}" />
<!--Redis密码-->
<constructor-arg index="4" name="password" value="#{'${redis.password}'!=''?'${redis.password}':null}" />
</bean>
</beans>
package com.wanggs.task;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.wanggs.mapper.UserMapper;
import com.wanggs.pojo.User;
import com.wanggs.statu.ActiveStatus;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;
import javax.mail.internet.MimeMessage;
import java.util.concurrent.TimeUnit;
/**
* @author Wgs
* @version 1.0
* @create:2017/11/28
*/
@Service
public class MailTask implements Runnable {
@Autowired
private UserMapper userMapper;
private static Logger LOG = LoggerFactory.getLogger(MailTask.class);
// 邮件缓存,发送激活用户的邮件,写进去时间,如果没有人访问就过期,有人访问就延长时间
private static Cache<String, String> cache = CacheBuilder.newBuilder()
.expireAfterWrite(6, TimeUnit.HOURS)
.build();
private String code;
private String email;
private JavaMailSender javaMailSender;
private int operation;
public MailTask() {
}
public MailTask(String code, String email, JavaMailSender javaMailSender, int operation) {
this.code = code;
this.email = email;
this.javaMailSender = javaMailSender;
this.operation = operation;
}
@Override
public void run() {
javaMailSender.send(new MimeMessagePreparator() {
@Override
public void prepare(MimeMessage mimeMessage) throws Exception {
LOG.info("开始发送邮件~");
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
mimeMessageHelper.setFrom("ceshihahu@yeah.net");
mimeMessageHelper.setTo(email);
mimeMessageHelper.setSubject("一封激活邮件~");
String url = "http://localhost:8083/user/";
StringBuffer sb = new StringBuffer();
sb.append("<html><head></head><body>");
if (operation == 1) {
sb.append("<a href=" + url + "activate.do?code=");
sb.append(code);
sb.append(">点击激活</a></body>");
} else {
String newPassword = code.substring(0,8);
cache.put("newPassword",newPassword);
sb.append("是否将您的密码修改为:");
sb.append(newPassword);
sb.append(",<a href=" + url + "verify.do?code=" + code + ">");
sb.append("点击是</a></body>");
}
// 放入缓存
cache.put(code, email);
mimeMessageHelper.setText(sb.toString(), true);
LOG.info("发送邮寄结束~");
}
});
}
/**
* 验证token
*
* @param code
*/
public String active(String code,String flg) {
String email = cache.getIfPresent(code);
// code 是否正确 是否过期 验证数据库是否存在 更新数据库 删除缓存
if (email == null) {
return "token过期或者无效";
}
User user = userMapper.selectByEmail(email);
if (user == null) {
return "帐号不存在";
}else {
user.setActived(ActiveStatus.ACTIVE.getValue());
if (StringUtils.isBlank(flg)) {
userMapper.updateActived(user);
}else {
String newPassword = cache.getIfPresent("newPassword");
user.setPassword(newPassword);
userMapper.updatePassword(user);
// 删除缓存
cache.invalidate("newPassword");
}
// 删除缓存中键对应的值
cache.invalidate(code);
LOG.info("帐号已经激活~");
}
return "ok";
}
}