java thread pool tool--ExecutorService, simple example

     Java multithreading has many uses in the java programming process, and is often asked during interviews. Here is a simple example of thread pool: operate User through thread pool, the code is as follows

    

    1. Entity class: User

package com.entity;

//entity class
public class User {
	private int id ;
	private String name;
	private String age;
	private String address;
	
	public User(){
		
	}
	
	public User(String name,String age,String address){
		this.name = name;
		this.age = age;
		this.address = address;
	}
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getAge() {
		return age;
	}
	
	public void setAge(String age) {
		this.age = age;
	}
	
	public String getAddress() {
		return address;
	}
	
	public void setAddress(String address) {
		this.address = address;
	}
}

 

 2. Database operation class: UserDao

package com.dao;

import com.entity.User;

public class UserDao {
	//Add user
	public void addUser(User user){
		//This method is used to manipulate data, save code, and do simple output
		System.out.println(user.getName());
		System.out.println(user.getAge());
	}
}

 

   3. Test calling class: UserServer

package com.serverlet;

import com.entity.User;
import com.thread.ThreadPool;
import com.thread.UserAddThread;

/**
 * User operation class, thread pool test
 */
public class UserServer {
	 //Add user
	 public void addUser() throws Exception{
		 User entity = new User("熊敏","28","深圳");
		 
		 // Stuff the user information into the thread pool
		 ThreadPool.getInstance().execute(new UserAddThread(entity));
	 }
	 
	 
	 //test
	 public static void main(String[] args) throws Exception {
		UserServer userServer = new UserServer();
		 
		for (int i = 0; i < 10; i++) {
			userServer.addUser();
		}
	 }
}

 

   4. Thread pool class: ThreadPool

package com.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Method to implement thread pool,
 */
public class ThreadPool {
	//JDK tool class, create a thread pool, the maximum number of links in this thread pool is 10
	final ExecutorService exec = Executors.newFixedThreadPool(10);
	
	private static ThreadPool pool = new ThreadPool();
	
	private ThreadPool(){
	}

	public static ThreadPool getInstance(){
		if(pool == null){
			return pool = new ThreadPool();
		}
		return pool;
	}
	
	
	/**
	 * Execute thread tasks
	 * @param command a thread task that needs to be executed
	 */
	public void execute(Runnable command) throws Exception{
		exec.submit(command);
	}
}

   

   5. Thread class (thread task class): UserAddThread

package com.thread;

import com.dao.UserDao;
import com.entity.User;

//thread class
public class UserAddThread implements Runnable{
	UserDao userDao = new UserDao();
	
	private User user;
	
	//When initializing, you need to instantiate the user too
	public UserAddThread(User user){
		this.user = user;
	}  
	
	
	// execute the task
	@Override
	public void run() {
		try{
			userDao.addUser(user);
		}catch(Exception e){
			e.printStackTrace ();
		}
	}
}

 

   See the attachment for a complete example.

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327041546&siteId=291194637