吉林大学java编程上机(Lab4)

Lab4_1

题目:Design a train ticket simulation program Ticket.java. If there are 100 train tickets to be sold at the railway station, now there are 5 ticketing outlets selling tickets at the same time, and 5 threads are used to simulate the ticketing situation at these 5 ticketing outlets. The requirements are as follows:
(1)Output the ticket number sold at each ticketing outlet;
(2)Train tickets with the same number cannot be sold at the these ticketing outlets.
An example of the output result is shown in the figure below. The number on the left is the ticketing outlet number, and the number on the right is the number of the train ticket sold.
5______________100
2______________99
4______________98
1______________97
3______________96
……
4______________3
3______________2
2______________1

分析
设计了一个火车票仿真程序ticket .java。假设火车站有100张火车票出售,现在有5个售票网点同时售票,用5个线程来模拟这5个售票网点的购票情况。具体要求如下:
(1)输出各票务网点售出的票号;
(2)相同数量的火车票不能在这些售票点出售。
输出结果的示例如下图所示。左边的号码是售票口号码,右边的号码是售出的火车票号码:
5______________100
2______________99
4______________98
1______________97
3______________96
……
4______________3
3______________2
2______________1

分析

写的时候雄赳赳气昂昂,结果自闭了,我看别人的博客全都是sythcheonized里面加sleep,我开始无法理解二者的关系,反正sleep放在里面结果输出就是不对,就像这样一直都是一个窗口抢占:
在这里插入图片描述

而看题目输出应该是12345个窗口均匀分布购票, 于是乎找外挂~~(请教大佬)~~,嗷!我觉得大佬说的很好:
在这里插入图片描述

  • sythcheonized在这里相当于是占用售票窗口
  • 把sleep放在sythcheonized里面相当于占用售票窗口然后等待 放在外面相当于离开售票窗口等待
  • 前者依然没有给其他人访问售票窗口的机会,后者才是正确的
  • 前者是买完票没有走开在窗口前等了一会然后又来了 其他人没有买票的机会 ;后者是买完票了离开窗口等了一会然后再来买票 这个给了其他人到窗口买票的机会
  • 如果你把sleep这行代码去掉 依然是正确的 只是张三进程买完票之后刚走又回来了 离开窗口的时间很短 导致其他人还没走到窗口,窗口又被张三占用了,所以对于这道题在外围加上sleep会使得输出更好看。

代码如下
ticket.java

package Lab4;

public class ticket implements Runnable {
	public int total_tickets=100;
	public int count=0;
	
	//重写run()函数
	public void run() {
		
		while(total_tickets>0) {
		    try {
				Thread.sleep(100);
			    synchronized(this) {//使用同步代码块
			    	if(total_tickets>0) {
			    		count++;
			    		System.out.println(Thread.currentThread().getName()+"______________"+count);
			    		total_tickets--;
			    	}
			     }
		       }catch(Exception e) {
			      e.printStackTrace();
		       }
	   }
	}
}

test.java

package Lab4;


public class test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//创建线程对象
		ticket x = new ticket();
		//启动五个线程
		for(int i=1;i<=5;i++) {
		  new Thread(x,""+i).start();
			}
		}
	}

输出:
在这里插入图片描述


Lab4_2

题目:Create database “myDB” with JavaDB, and create table “employee” in it as table-1. Then input the corresponding data as table-2.
(1) Display all records of male employees in the “employee” table.
(2) Add a data record to “employee” table: “2017, Xing SF, female, 650”.
(3) Change the “salary” of the record where “sno” is “2017” to “900” and output the result.

在这里插入图片描述

使用JavaDB创建数据库“myDB”,并在其中创建表“employee”作为表1。然后输入表2所示的相应数据。
(1)显示“employee”表中所有男性员工的记录。
(2)在“员工”表中增加一条数据记录:“2017,邢顺丰,女,650”。
(3)将“sno”为“2017”的记录中的“salary”改为“900”,输出结果。

这道题里面值得注意的就是:
看博客吧,大致流程就是去官网下JavaDB然后配置环境变量,再在cmd上用ij命令建表,执行相应的增删改查。
官方下载地址
配置环境+相应操作
我是跟着这篇博客做的
值得一提的是:我把安装包解压之后,在c盘java文件里新建了个文件夹db,然后将安装包所有内容拷贝了一份过去,后来环境变量明明配好了就是不能直接通过:
在这里插入图片描述
然后灵机一动,直接进入c盘对应db/bin的目录按cmd
在这里插入图片描述
之后就好了,我猜测可能是c盘需要管理员权限吧。
在这里插入图片描述

具体操作如下:
建文件夹:
在这里插入图片描述
建表:
在这里插入图片描述
显示表的内容:
在这里插入图片描述

查询男员工:
在这里插入图片描述

插入:
在这里插入图片描述
修改:
在这里插入图片描述
断开连接:disconnect
hu,十二点了欸,明天继续!


Lab4_3

题目:Design a client/server communication program based on stream sockets. The requirements are as follows:
(1) Write the server-side program and the client-side program respectively (the port number is 1888).
(2) The client sends information to the server, and the server receives the information and displays it.
(3) At the same time, the server will feedback the information to the client, and the client will receive the information and display it.
Please submit program code and the screenshot of the program output in the answer.

设计一个基于流套接字的客户端/服务器通信程序。具体要求如下:
(1)分别编写服务器端程序和客户端程序(端口号1888)。
(2)客户端向服务器发送信息,服务器接收并显示。
(3)同时,服务器将信息反馈给客户端,客户端接收到信息并显示。

这道题里面值得注意的就是:

代码如下
服务器端:

package Lab4;
import java.net.*;
import java.io.*;

public class CS_Server {
	public static void main(String[] args) throws Exception
	{
		ServerSocket ss = new ServerSocket(38380);//是一个能够接受其他通信实体请求的类
		System.out.println("服务器正在等待客户端的连接请求----");
		//用一个while循环可以同时响应多个客户端的请求
		while(true){
			 Socket sk= ss.accept();//服务器监听对应端口的输入
			 ServerThread  st = new ServerThread(sk);//创建一个线程,用线程创建一个套接字
			 st.start();
		}
	}


}
//服务器线程类
class ServerThread extends Thread
{
	Socket sk;
	public ServerThread(Socket sk){
	this.sk= sk;
	}
	public void run() {		
		BufferedReader br=null;
		try{
		br  = new BufferedReader(new InputStreamReader(sk.getInputStream()));
		String line = br.readLine();
		System.out.println("来自客户端的数据:"+line);
		br.close();
		sk.close();
		}
		catch(IOException e){
			e.printStackTrace();
		}
	}
}

客户端:

package Lab4;
import java.net.*;
import java.io.*;
import java.util.Scanner;
public class CS_Client {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try{
			Socket sk =new Socket("192.168.43.32",38380);
			System.out.println("客户端已经开启----");
			PrintStream ps = new PrintStream(sk.getOutputStream());//将客户端套接字的输出流用printStream包装起来,类似于C语言中的fprintf类型转换
			System.out.print("请输入需要发送到服务器的内容:");
			Scanner sn = new Scanner(System.in);
			String str = sn.nextLine();
			ps.println(str);//把控制台输入的内容送入被printstream类包装的输出流里面	
			ps.close();//关闭输出流包装
			sk.close();//关闭socket套接字,已经传完数据,才能关闭
			}
			catch(Exception e){
				e.printStackTrace();
			}
	}

}

请添加图片描述
请添加图片描述
请添加图片描述

  • 3
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值