实验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
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值