20200226,感觉自己写不动笔记了,想快点把知识点学完,啊,好累啊啊啊啊啊
一般遇到觉得自己容易忘记或者觉得没遇到过的知识点会记笔记,这次的一些笔记是在代码里做的,下面贴一些代码:
希望本社畜快点恢复精神然后冲冲冲!
一、多线程
关于多线程:多线程即在同一时间,可有做许多事情。
创建多线程有三种方式:分别是继承线程类,实现Runnable接口,匿名类。
1.关于线程池
1.1自己写线程池并测试:
ThreadPool.java
package multiplethread;
import java.util.LinkedList;
import org.omg.CORBA.PUBLIC_MEMBER;
/**
* 自定义开发一个线程池
* @author 86134
*
*/
public class ThreadPool {
//线程池大小
int threadPoolSize;
//任务容器
LinkedList<Runnable> tasks = new LinkedList<Runnable>();
//试图消费任务的线程
public ThreadPool() {
threadPoolSize=2;
//启动10个任务消费线程
synchronized (tasks) {
for (int i = 0; i < threadPoolSize; i++) {
new TaskConsumeThread("任务消费线程"+i).start();
}
}
}
public void add(Runnable r) {
synchronized (tasks) {
tasks.add(r);
//唤醒等待的任务消费者线程
tasks.notifyAll();
}
}
class TaskConsumeThread extends Thread{
public TaskConsumeThread(String name) {
super(name);
}
Runnable task;
public void run() {
System.out.println("启动:"+this.getName());
while (true) {
synchronized (tasks) {
while (tasks.isEmpty()) {
try {
tasks.wait();
System.out.println("等待中……");
} catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
}
task=tasks.removeLast();
//允许添加任务的线程可以继续添加任务
tasks.notifyAll();
System.out.println("唤醒所有");
}
System.out.println(this.getName()+"获取到任务,并执行");
task.run();
}
}
}
}
TestThread.java
package multiplethread;
public class TestThread {
public static void main(String[] args) {
ThreadPool pool = new ThreadPool();
for (int i = 0; i < 2; i++) {
Runnable task=new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("正在执行任务");
}
};
pool.add(task);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
}
1.2使用java自带的线程池
TestJavaThreadPool.java
package multiplethread;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class TestJavaThreadPool {
public static void main(String[] args) throws InterruptedException{
ThreadPoolExecutor threadpool = new ThreadPoolExecutor(10, 15, 60, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>());//第四个参数用来放任务的集合
threadpool.execute(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("任务1");
}
});
}
}
二、数据库
1.DAO接口 将数据库的方法卸载DAO中,不用具体实现
DAO.java
package jdbc;
/**
* DAO接口
* DAO=DataAccess Object 数据访问对象
* 把数据库相关的操作都封装在这个类里面,其他地方看不到JDBC的代码
* @author 86134
*
*/
import java.util.List;
public interface DAO {
//增加
public void add(Hero hero);
//修改
public void update(Hero hero);
//删除
public void delete(int id);
//获取
public Hero get(int id);
//查询
public List<Hero> list();
//分页查询
public List<Hero>list(int start,int count);
}
2.这个代码用来实现接口,完成指定功能
Hero.DAO
package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.print.attribute.standard.MediaSize.NA;
import com.mysql.cj.xdevapi.Result;
/**
* 实现接口DAO
* @author 86134
*
*/
public class HeroDAO implements DAO{
//因为驱动初始化只需要执行一次,
//因此把驱动的初始化放在了构造方法HeroDAO里比较合适
public HeroDAO() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("1.数据库驱动加载成功");
} catch (ClassNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
}
}
//提供了一个getConnection方法返回连接
public Connection getConnection()throws SQLException{
return DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/databasename?serverTimezone=UTC&characterEncoding=utf-8",
"root","password");
}
public int getTotal() {
int total=0;
try(Connection c=getConnection();
Statement s=c.createStatement();) {
String sql="select count(*) from Hero";
ResultSet rs=s.executeQuery(sql);
if(rs.next()) {
total=rs.getInt(1);
}
System.out.println("total:"+total);
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
return total;
}
@Override
public void add(Hero hero) {
// TODO Auto-generated method stub
String sqlString="insert into hero values(null,?,?,?)";
try (Connection connection=getConnection();
PreparedStatement pStatement=connection.prepareStatement(sqlString,Statement.RETURN_GENERATED_KEYS)){
{
pStatement.setString(1, hero.name);
pStatement.setFloat(2, hero.hp);
pStatement.setInt(3, hero.damage);
pStatement.execute();
ResultSet rs = pStatement.getGeneratedKeys();
if(rs.next()) {
int id=rs.getInt(1);
String string=rs.getString(2);
hero.id=id;
}
}
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
}
@Override
public void update(Hero hero) {
// TODO Auto-generated method stub
String sqlString="update hero set name=?,hp=?,damage=? where id=?";
try (Connection connection=getConnection();
PreparedStatement ps=connection.prepareStatement(sqlString);){
{
ps.setString(1, hero.name);
ps.setFloat(2, hero.hp);
ps.setInt(3, hero.damage);
ps.setInt(4, hero.id);
ps.execute();
}
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
}
@Override
public void delete(int id) {
// TODO Auto-generated method stub
try(Connection connection=getConnection();
Statement statement=connection.createStatement();) {
String sql ="delete from hero where id="+id;
statement.execute(sql);
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
}
@Override
public Hero get(int id) {
Hero hero =null;
// TODO Auto-generated method stub
try(Connection connection=getConnection();
Statement statement=connection.createStatement();) {
String sql="select * from hero where id="+id;
ResultSet rs=statement.executeQuery(sql);
if(rs.next()) {
hero = new Hero();
String name= rs.getString(2);
Float hp=rs.getFloat(3);
int damage=rs.getInt(4);
hero.name=name;
hero.hp=hp;
hero.damage=damage;
hero.id=id;
}
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
return hero;
}
@Override
public List<Hero> list() {
// TODO Auto-generated method stub
return list(0,Short.MAX_VALUE);
}
@Override
public List<Hero> list(int start, int count) {
// TODO Auto-generated method stub
List<Hero> heros = new ArrayList<Hero>();
String sql="select * from hero order by id desc limit ?,?";
try(Connection connection=getConnection();
PreparedStatement ps=connection.prepareStatement(sql);){
ps.setInt(1, start);
ps.setInt(2, count);
ResultSet rs=ps.executeQuery();
while(rs.next()) {
Hero hero = new Hero();
int id=rs.getInt(1);
String name=rs.getString(2);
float hp=rs.getFloat(3);
int damage=rs.getInt(4);
hero.id=id;
hero.name=name;
hero.hp=hp;
hero.damage=damage;
heros.add(hero);
}
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
return heros;
}
}