知识点
Java8 Lambda
Lambda 表达式,也可称为闭包,它是推动 Java 8 发布的最重要新特性。
Lambda 允许把函数作为一个方法的参数(函数作为参数传递进方法中)。
使用 Lambda 表达式可以使代码变的更加简洁紧凑。
格式:
(parameters) -> expression
或
(parameters) ->{ statements; }
return distinctProjects.stream().map(projectVO -> {
ProjectInfoResponse projectInfoResponse = new ProjectInfoResponse();
projectInfoResponse.setDescription(projectVO.getDescription());
projectInfoResponse.setLogoColor(projectVO.getLogoColor());
projectInfoResponse.setName(projectVO.getName());
projectInfoResponse.setUserId(userId + "");
projectInfoResponse.setRole(AuthRole.PROJECT_ADMIN);
projectInfoResponse.setUuid(projectVO.getUuid());
return projectInfoResponse;
}).collect(Collectors.toList());
更复杂的例子:
@Override
public List<XlyProjectComponentBean> queryProjectComponentBean(String company, String project) {
List<Application> list1 = applicationService.queryApplications(company, true, false);
List<Application> list2 = applicationService.queryApplications(company, false, true);
List<Privilege> privilegeList = queryProjectPrivilegeComponent(company, project);
return list1.stream().map(app -> {
XlyProjectComponentBean bean = new XlyProjectComponentBean();
bean.setCompanyUuid(company);
bean.setProjectUuid(project);
bean.setId(app.getId());
bean.setName(app.getName());
bean.setNum(app.getNum());
// CHECKSTYLE:OFF
bean.setLevel(1);
bean.setKey(app.getCode());
privilegeList.forEach(p -> {
if (p.getDeleteStatus() == 0 && p.getPermissionAppId().equals(app.getId())) {
bean.setIschecked(true);
}
});
// 组装二级组件
List<XlyProjectComponentBean> childrenBeanList = new ArrayList<>();
list2.forEach(appc -> {
if (appc.getParentId().equals(app.getId())) {
XlyProjectComponentBean beanChild = new XlyProjectComponentBean();
beanChild.setCompanyUuid(company);
beanChild.setProjectUuid(project);
beanChild.setId(appc.getId());
beanChild.setName(appc.getName());
// CHECKSTYLE:OFF
beanChild.setLevel(2);
beanChild.setNum(appc.getNum());
beanChild.setKey(appc.getCode());
privilegeList.forEach(p -> {
if (p.getDeleteStatus() == 0 && p.getPermissionAppId().equals(appc.getId())) {
beanChild.setIschecked(true);
}
});
childrenBeanList.add(beanChild);
}
});
bean.setChildren(childrenBeanList);
return bean;
}).sorted(Comparator.comparing(XlyProjectComponentBean::getNum)).collect(Collectors.toList());
}
Thread
- What are Java Threads?
-
Facility to allow multiple activities within a single process
Referred as lightweight process -
A thread is a series of executed statements
-
Each thread has its own program counter, stack and local
variables -
A thread is a nested sequence of method callsIts shares
memory, files and per-process state
- Whats the need of a thread or why we use Threads?
- To perform asynchronous or background processing
- Increases the responsiveness of GUI applications
- Take advantage of multiprocessor systems
- Simplify program logic when there are multiple independent entities
- What happens when a thread is invoked?
When a thread is invoked, there will be two paths of execution. One path will execute the thread and the other path will follow the statement after the thread invocation. There will be a separate stack and memory space for each thread.
MultiThread
Let’s summarize the discussion in points:
- The main purpose of multithreading is to provide simultaneous execution of two or more parts of a program to maximum utilize the CPU time. A multithreaded program contains two or more parts that can run concurrently. Each such part of a program called thread.2. Threads are lightweight sub-processes, they share the common memory space. In Multithreaded environment, programs that are benefited from multithreading, utilize the maximum CPU time so that the idle time can be kept to minimum.3. A thread can be in one of the following states:
NEW – A thread that has not yet started is in this state.
RUNNABLE – A thread executing in the Java virtual machine is in this state.
BLOCKED – A thread that is blocked waiting for a monitor lock is in this state.
WAITING – A thread that is waiting indefinitely for another thread to perform a particular action is in this state.
TIMED_WAITING – A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state.
TERMINATED – A thread that has exited is in this state.
A thread can be in only one state at a given point in time.
泛型嵌套
@GetMapping("/code/application/{application}")
@ApiOperation(value = "code全局设置权限列表", notes = "code全局设置权限列表")
public ResponseEntity<AdminResponse<PageBean<PermissionApplicationResponse>>> queryCodeList(
@PathVariable String company,
@PathVariable(value = "application") String application,
@PathVariable(value = "project") String project,
@RequestParam(value = "page", defaultValue = "1") Integer page,
@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
return ResponseEntity.status(HttpStatus.OK)
.body(AdminResponse.result(GlobalConstant.SUCCESS, "授权列表查询成功!",
permissionService.queryPermissionList2(company, project, application, page, pageSize, true)));
}