Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

blog-dev-011-multi-thread #159

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.yen.mdblog.config;

// https://blog.csdn.net/u010986241/article/details/138205146

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
public class ThreadPoolConfig {

int CORE_POOL_SIZE = 5;
int MAX_POOL_SIZE = 10;
int QUEUE_CAPACITY = 20;
int KEEP_ALIVE_SECONDS = 30;
String THREAD_NAME_PREFIX = "custom-thread-x-";

@Bean(name="customThreadPool")
public ThreadPoolTaskExecutor threadPoolExecutor(){

System.out.println(">>>> ThreadPoolConfig init");
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();

executor.setCorePoolSize(CORE_POOL_SIZE);
executor.setMaxPoolSize(MAX_POOL_SIZE);
executor.setQueueCapacity(QUEUE_CAPACITY);
executor.setKeepAliveSeconds(KEEP_ALIVE_SECONDS);
executor.setThreadNamePrefix(THREAD_NAME_PREFIX);
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());

return executor;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.yen.mdblog.service.AuthorService;
import java.security.Principal;
import java.util.List;
import java.util.concurrent.ExecutionException;

import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
Expand All @@ -20,7 +22,7 @@ public class AuthorController {
@Autowired AuthorService authorService;

@GetMapping("/all")
String getAllAuthor(Model model, Principal principal) {
String getAllAuthor(Model model, Principal principal) throws ExecutionException, InterruptedException {

List<Author> authors = authorService.getAllAuthors();
model.addAttribute("authors", authors);
Expand All @@ -29,7 +31,7 @@ String getAllAuthor(Model model, Principal principal) {
}

@GetMapping("/{id}")
public String getAuthorById(@PathVariable Integer id, Model model, Principal principal) {
public String getAuthorById(@PathVariable Integer id, Model model, Principal principal) throws ExecutionException, InterruptedException {

Author author = authorService.getById(id);
model.addAttribute("author", author);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.yen.mdblog.service.AuthorService;
import java.security.Principal;
import java.util.List;
import java.util.concurrent.ExecutionException;

import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
Expand Down Expand Up @@ -43,6 +45,10 @@ public String preEdit(Model model, Principal principal) {
authorList = authorService.getAllAuthors();
pageInfo = new PageInfo<Author>(authorList, PageConst.PAGE_SIZE.getSize());
model.addAttribute("pageInfo", pageInfo);
} catch (ExecutionException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
PageHelper.clearPage();
}
Expand All @@ -66,7 +72,7 @@ public String editAuthor(Model model, Principal principal) {
}

@GetMapping("/{id}")
public String getAuthorById(@PathVariable Integer id, Model model, Principal principal) {
public String getAuthorById(@PathVariable Integer id, Model model, Principal principal) throws ExecutionException, InterruptedException {

Author author = authorService.getById(id);
model.addAttribute("author", author);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
Expand Down Expand Up @@ -125,7 +126,7 @@ public String createPostForm(Model model, Principal principal) {
}

@RequestMapping(value = "/create", method = RequestMethod.POST)
public String createPost(CreatePost request, Model model, Principal principal) {
public String createPost(CreatePost request, Model model, Principal principal) throws ExecutionException, InterruptedException {

log.info(">>> create post start ...");
Post post = new Post();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import com.yen.mdblog.entity.Po.Author;

import java.util.List;
import java.util.concurrent.ExecutionException;

public interface AuthorService {

Author getById(Integer id);
Author getById(Integer id) throws ExecutionException, InterruptedException;

Author getByName(String name);

List<Author> getAllAuthors();
List<Author> getAllAuthors() throws ExecutionException, InterruptedException;

Boolean createAuthor(String name, String email);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.yen.mdblog.service.impl;

import com.yen.mdblog.entity.Po.Author;
import com.yen.mdblog.mapper.AuthorMapper;
import com.yen.mdblog.service.AuthorService;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

@Service
@Log4j2
public class AsyncAuthorServiceImpl implements AuthorService {


/** Thread pool */
@Autowired
@Qualifier("customThreadPool")
private ThreadPoolTaskExecutor threadPoolTaskExecutor;

@Autowired AuthorMapper authorMapper;

@Override
public Author getById(Integer id) throws ExecutionException, InterruptedException {

Future<Author> futureAuthor = threadPoolTaskExecutor.submit(() -> {

System.out.println("--> Thread name : " + Thread.currentThread().getName() + ", id = " + Thread.currentThread().getId());

return authorMapper.getById(id); // Assuming authorMapper.getById(id) returns Author
});

return futureAuthor.get(); // This will block until the result is available
}

@Override
public Author getByName(String name) {

return authorMapper.getByName(name);
}


@Override
public List<Author> getAllAuthors() throws ExecutionException, InterruptedException {

Future<List<Author>> allAuthors = threadPoolTaskExecutor.submit(() -> {

System.out.println("--> Thread name : " + Thread.currentThread().getName() + ", id = " + Thread.currentThread().getId());

return authorMapper.getAllAuthors();
}
);

return allAuthors.get();
}

@Override
public Boolean createAuthor(String name, String email) {
try {
Author newAuthor = new Author();
int id = authorMapper.getAuthorCount() + 1;
newAuthor.setId(id);
newAuthor.setName(name);
newAuthor.setEmail(email);
newAuthor.setCreateTime(new Date());
newAuthor.setUpdateTime(new Date());
log.info("save author = " + newAuthor);
authorMapper.insertAuthor(newAuthor);
log.info("create new user OK ...");
return true;
} catch (Exception e) {
log.error("createAuthor failed : " + e);
return false;
}
}

@Override
public void updateAuthor(Author author) {

authorMapper.updateAuthor(author);
}

}
Original file line number Diff line number Diff line change
@@ -1,61 +1,61 @@
package com.yen.mdblog.service.impl;

import com.yen.mdblog.entity.Po.Author;
import com.yen.mdblog.mapper.AuthorMapper;
import com.yen.mdblog.service.AuthorService;
import java.util.Date;
import java.util.List;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Log4j2
public class AuthorServiceImpl implements AuthorService {

@Autowired AuthorMapper authorMapper;

@Override
public Author getById(Integer id) {

return authorMapper.getById(id);
}

@Override
public Author getByName(String name) {

return authorMapper.getByName(name);
}

@Override
public List<Author> getAllAuthors() {

return authorMapper.getAllAuthors();
}

@Override
public Boolean createAuthor(String name, String email) {
try {
Author newAuthor = new Author();
int id = authorMapper.getAuthorCount() + 1;
newAuthor.setId(id);
newAuthor.setName(name);
newAuthor.setEmail(email);
newAuthor.setCreateTime(new Date());
newAuthor.setUpdateTime(new Date());
log.info("save author = " + newAuthor);
authorMapper.insertAuthor(newAuthor);
log.info("create new user OK ...");
return true;
} catch (Exception e) {
log.error("createAuthor failed : " + e);
return false;
}
}

@Override
public void updateAuthor(Author author) {

authorMapper.updateAuthor(author);
}
}
//package com.yen.mdblog.service.impl;
//
//import com.yen.mdblog.entity.Po.Author;
//import com.yen.mdblog.mapper.AuthorMapper;
//import com.yen.mdblog.service.AuthorService;
//import java.util.Date;
//import java.util.List;
//import lombok.extern.log4j.Log4j2;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//@Service
//@Log4j2
//public class AuthorServiceImpl implements AuthorService {
//
// @Autowired AuthorMapper authorMapper;
//
// @Override
// public Author getById(Integer id) {
//
// return authorMapper.getById(id);
// }
//
// @Override
// public Author getByName(String name) {
//
// return authorMapper.getByName(name);
// }
//
// @Override
// public List<Author> getAllAuthors() {
//
// return authorMapper.getAllAuthors();
// }
//
// @Override
// public Boolean createAuthor(String name, String email) {
// try {
// Author newAuthor = new Author();
// int id = authorMapper.getAuthorCount() + 1;
// newAuthor.setId(id);
// newAuthor.setName(name);
// newAuthor.setEmail(email);
// newAuthor.setCreateTime(new Date());
// newAuthor.setUpdateTime(new Date());
// log.info("save author = " + newAuthor);
// authorMapper.insertAuthor(newAuthor);
// log.info("create new user OK ...");
// return true;
// } catch (Exception e) {
// log.error("createAuthor failed : " + e);
// return false;
// }
// }
//
// @Override
// public void updateAuthor(Author author) {
//
// authorMapper.updateAuthor(author);
// }
//}
Loading