实验7 多线程与JDBC

1. 实验目的

了解JAVA多线程编程的概念;熟悉创建多线程的两种方法;了解数据库编程的基本过程。

2. 实验要求

1) 创建多线程

创建2个线程,一个线程负责输出英文字母表,另一个线程负责输出希腊字母表。

要求:
①通过继承Thread类实现创建线程。
②通过实现Runnable接口创建线程。

package t7_1;

public class mymain7_1_1 {
	public static void main(String args[]){
		yingwen y = new yingwen();
		xila x = new xila();
		y.start();
		x.start();
	}
}

class yingwen extends Thread{
	public void run(){
		for(int i = 97; i <= 122; i++){
			System.out.printf("%c", i);		
		}
	}
}

class xila extends Thread{
	public void run(){
		for(int i = 945; i <= 969; i++){
			System.out.printf("%c", i);		
		}
	}
}
package t7_1;

public class mymain7_1_2 {
	public static void main(String args[]){
		Thread y = new Thread(new yingwen2());
		Thread x = new Thread(new xila2());
		y.start();
		x.start();
	}
}

class yingwen2 implements Runnable{
	public void run(){
		for(int i = 97; i <= 122; i++){
			System.out.printf("%c", i);
		}
	}
}

class xila2 implements Runnable{
	public void run(){
		for(int i = 945; i <= 969; i++){
			System.out.printf("%c", i);
		}
	}
}

2) 线程同步

对于题目1,要求通过线程同步,保证一个线程输出完字母表之后,另一个线程再执行。

package t7_2;

public class mymain7_2 {
	public static void main(String args[]){
		Printer p = new Printer(); //只能创建一个Printer对象
		Thread t1 = new Thread(p);
		Thread t2 = new Thread(p);
		t1.setName("英文字母");
		t2.setName("希腊字母");
		t1.start();
		t2.start();				
	}
}

class Printer implements Runnable{
	public synchronized void printer(){
		if(Thread.currentThread().getName().equals("英文字母")){
			for(char c = 'a'; c <= 'z'; c++){
				System.out.printf("%c", c);
				try { Thread.sleep(100); } 
				catch(InterruptedException e){}
			}
			System.out.print('\n');
		}
		else if(Thread.currentThread().getName().equals("希腊字母")){
			//一种新方法,但是电脑键盘没法打出希腊字母
			for(char c = 'α'; c <= 'ω'; c++){
				System.out.printf("%c", c);
				try { Thread.sleep(100); } 
				catch(InterruptedException e){}
			}
			System.out.print('\n');
		}
	}
	public void run(){
		if(Thread.currentThread().getName().equals("英文字母")){
			printer();
		}
		else if(Thread.currentThread().getName().equals("希腊字母")){
			printer();
		}
	}
}

最开始做的,好像哪里不对

package t7_2;

public class mymain7_2_1 {
	public static void main(String args[]){
		yingwen y = new yingwen();
		xila x = new xila();
		y.start();
		x.start();
	}
}

class yingwen extends Thread{
	public synchronized void run(){
		for(int i = 97; i <= 122; i++){
			System.out.printf("%c", i);		
		}
	}
}

class xila extends Thread{
	public synchronized void run(){
		for(int i = 945; i <= 969; i++){
			System.out.printf("%c", i);		
		}
	}
}
package t7_2;

public class mymain7_2_2 {
	public static void main(String args[]){
		Thread y = new Thread(new yingwen2());
		Thread x = new Thread(new xila2());
		y.start();
		x.start();
	}
}

class yingwen2 implements Runnable{
	public synchronized void run(){
		for(int i = 97; i <= 122; i++){
			System.out.printf("%c", i);
		}
	}
}

class xila2 implements Runnable{
	public synchronized void run(){
		for(int i = 945; i <= 969; i++){
			System.out.printf("%c", i);
		}
	}
}

3) 多线程实现键盘操作练习

编写一个JAVA应用程序,在主线程中再创建两个线程,一个线程负责给出键盘上字母键上的字母(每5秒随机给出一个字母),另一个线程负责让用户输入给出的字母,输入正确,分数加1,输错,分数减1,输入#结束程序。程序运行的效果如下:
在这里插入图片描述

package t7_3;

import java.util.Scanner;
import java.util.Random;

public class mymain7_3 {
	public static void main(String args[]){
		System.out.print("打字练习(输入#)结束"+"\n");
		System.out.print("输入显示的字母并回车"+"\n");
		System.out.print("\n");
		Letter l = new Letter();
		GiveLetter g = new GiveLetter();
		InputLetter i = new InputLetter();
		g.setLetter(l);
		i.setLetter(l);
		g.start();
		i.start();
	}
}

class Letter{	
	char c;
	public void setchar(char a){
		c = a;
	}
	public char getchar(){		
		return c;
	}
}

class GiveLetter extends Thread{
	Letter letter;
	public void setLetter(Letter l){
		letter = l;
	}
	public void run(){	
		while(true){					
			Random r = new Random();		
    		int l = 'a' + r.nextInt(26);
    		char c = (char)l;
    		letter.setchar(c);
			System.out.print("显示的字符:"+c+"\n");
			try{
				Thread.sleep(5000);			
			}
			catch(InterruptedException e){}				
		}
	}
}

class InputLetter extends Thread{	
	Letter letter;
	public void setLetter(Letter l){
		letter = l;
	}
	int score = 0;
	public void run(){
		while(true){
			Scanner reader = new Scanner(System.in);
			char a = reader.next().charAt(0);
			//reader.close(); //!!!一定不要关掉!!!
			
			if(a == letter.getchar()){
				score++;
				System.out.print("输入正确,目前分数"+score+"\n");
			}
			else if(a == '#'){
				System.exit(0);
			}
			else{
				score--;
				System.out.print("输入错误,目前分数"+score+"\n");
			}
			
		}		
	}
}

4) 数据库编程

将学生的考试成绩信息(包括学号,姓名,课程名,成绩等)存放到数据库中,然后编程实现:

①往成绩表中插入一门课成绩;
②删除某学生某门课成绩;
③修改某学生某门课成绩;
④查询某门课最高成绩等操作。
不考

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现多线程方式插入数据库,可以使用线程池来管理线程的创建和执行。首先,需要配置一个线程池,可以使用Spring的ThreadPoolTaskExecutor来创建。在配置类中,使用@EnableAsync注解开启异步执行功能,并在@Bean注解的方法中创建并配置ThreadPoolTaskExecutor对象。可以设置核心线程数、最大线程数、队列大小等参数,以及线程池中线程的名称前缀和拒绝策略。配置完成后,调用initialize()方法进行初始化,并将创建的线程池对象返回。 在具体的业务逻辑中,可以使用@Async注解将需要异步执行的方法标记为异步方法。在这些方法中,可以使用JDBC方式插入数据库。通过调用线程池的execute()方法,将需要执行的任务提交给线程池进行处理。线程池会根据配置的参数来管理线程的创建和执行,保证并发插入数据库的效率和线程的复用。 需要注意的是,在使用多线程插入数据库时,要确保数据库连接的线程安全性,可以使用连接池来管理数据库连接,以避免线程间的竞争和资源浪费。 引用\[1\]中的代码片段展示了一个配置线程池的示例,可以根据具体需求进行修改和扩展。 #### 引用[.reference_title] - *1* [多线程批量插入数据库](https://blog.csdn.net/qq_41315539/article/details/120562395)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值