本文主要是介绍—考核小结—,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
线程控制
public class Main extends Thread {Class lock = Main.class;private static int num = 0;private final int threadId;private final char c;public Main(int threadId, char character) {this.threadId = threadId;this.c = character;}public void run() {while (true) {synchronized (lock) {while (num % 3 != threadId) {try {lock.wait();} catch (InterruptedException e) {Thread.currentThread().interrupt();}}System.out.print(c);num++;lock.notifyAll();}}}public static void main(String[] args) {Thread t1 = new Main(0, 'f');Thread t2 = new Main(1, 'j');Thread t3 = new Main(2, 'w');t1.start();t2.start();t3.start();}
}
原本是想通过wait()实现顺序控制,但发现只能控制一个线程和其他两个线程的相对有序,不能同时保证三个线程的有序,于是想到了让三个线程抢同一个资源,通过targeid和id来保证线程的有序性,如果两个值不相等则释放资源,直到相当id++,就保证了线程有序。
并发安全的map
当时是手写了一个map,不过由于时间原因,只完成了增加和查询功能,修改和查询未能完成,后来经过提醒,想到了可以直接在map外层包裹一层synchronized
import java.util.*;public class map<K,V> extends HashMap<K,V> {private final Object lock = new Object();@Overridepublic V put(K key, V value) {synchronized (lock) {return super.put(key, value);}}@Overridepublic V get(Object key) {synchronized (lock) {return super.get(key);}}@Overridepublic V remove(Object key) {synchronized (lock) {return super.remove(key);}}@Overridepublic void clear() {synchronized (lock) {super.clear();}}@Overridepublic boolean containsKey(Object key) {synchronized (lock) {return super.containsKey(key);}}@Overridepublic boolean containsValue(Object value) {synchronized (lock) {return super.containsValue(value);}}@Overridepublic int size() {synchronized (lock) {return super.size();}}@Overridepublic boolean isEmpty() {synchronized (lock) {return super.isEmpty();}}
}
刚开始是写成object的,后来改为了泛型,实现了进一步解耦。
简单web服务器
要求:
1.更改Tomcat端口号为8888
2.Mysql表结构id , name ,password , time,字段自己决定
3.访问路径localhost:8888/fjw
4.Post:localhost:8888/fjw/register当前端传来一个name,password保存在数据库中,并且time为创建时间 Get:localhost:8888/fjw/getUser ,当前端发送get请求,返回这个表里面所有的用户信息
1.更改端口号
1.Tomacat安装目录下去server.xml文件中找到Connector标签将其port改为8888
<Connector port="8888" protocol="HTTP/1.1"connectionTimeout="20000"redirectPort="8443" />
我当时是直接在idea上改的端口号,想到去配置文件中更改但是不知道在哪里。
2.创建表结构,use类
CREATE SCHEMA mydatabase;USE mydatabase;CREATE TABLE users (id INT,name VARCHAR(50),password VARCHAR(50),time TIMESTAMP
);
这里我使用了dategrip数据库可视化工具,如果使用其他工具或终端操作,需要将
CREATE SCHEMA mydatabase;改为CREATE DATABASE mydatabase;
package com.lbdking.pojo;
public class User {private int id;private String name;private String password;private String time;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name){this.name = name;}public void setPassword(String Password) {this.password = password;}public String getPassword() {return password;}public void setTime(String time) {this.time = time;}public String getTime() {return time;}
}
3.配置数据库连接
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource"><property name="jdbcUrl" value="jdbc:mysql://localhost:3306/user_db"/><property name="username" value="root"/><property name="password" value="123456"/> <!-- 填写你密码啊,123456不是我密码 --><property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
</bean><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"/><property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.yourpackage.mapper"/>
</bean>
Mapper层
UserMapper.java
public interface UserMapper {void insertUser(User user);List<User> getAllUsers();
}
UserMapper.xml
<mapper namespace="com.yourpackage.mapper.UserMapper"><insert id="insertUser">INSERT INTO user (name, password, time) VALUES (#{name}, #{password}, #{time})</insert><select id="getAllUsers" resultType="com.yourpackage.model.User">SELECT * FROM user</select>
</mapper>
Service层
user接口
UserService.java
public interface UserService {void registerUser(User user);List<User> getAllUsers();
}
对应接口继承实现功能调用
UserServiceImpl.java
@Service
public class UserServiceImpl implements UserService {@Autowiredprivate UserMapper userMapper;@Overridepublic void registerUser(User user) {userMapper.insertUser(user);}@Overridepublic List<User> getAllUsers() {return userMapper.getAllUsers();}
}
Controller层
UserController.java
@RestController
@RequestMapping("/fjw")
public class UserController {@Autowiredprivate UserService userService;@PostMapping("/register")public String registerUser(@RequestParam String name, @RequestParam String password) {User user = new User();user.setName(name);user.setPassword(password);userService.registerUser(user);return "User registered successfully";}@GetMapping("/getUser")public List<User> getAllUsers() {return userService.getAllUsers();}
}
功能实现,@RequestMapping注解实现路径更改内层方法使用@PostMapping(“/register”)和@GetMapping(“/getUser”)实现post方法和get方法提交。
这篇关于—考核小结—的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!