zoukankan      html  css  js  c++  java
  • Java16周作业

    题目1:编写一个应用程序,利用Java多线程机制,实现时间的同步输出显示。

    /*使用Runnable接口使用类创建线程对象,重写run()方法**/

    代码

    public class timetext {
    
    	
    	public static void main(String[] args) {
    		Thread thread =new Thread(new time());
    		thread.start();
    
    	}
    
    }
    

      

    import java.util.Date;
    
    
    public class time implements Runnable {
    
    	public void run() {
    		Date date =null;
    		while(true){
    			date=new Date();
    			System.out.println(date);
    			try {
    				Thread.sleep(1000);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}
    }
    
    }
    

      运行结果

    编写一个应用程序,利用Java多线程机制,实现猜数字游戏(随机数范围0~100之间的整数)。

    代码

    public class Test {
    
    
    	public static void main(String[] args) {
    		game ga=new game();
    		ga.rand.start();
    		ga.input.start();
    		
    	}
    
    }
    

      

    import java.util.*;
    public class game implements Runnable {
    	 Thread rand,input; 
    	 int inputnum,random,flag;
    	 boolean a=false,b=false;
    	  public game(){
    		  rand=new Thread(this);
    		  input=new Thread(this);
    	    }
    	
    	public void run() {
    		 while(true){
    	            compare(); 
    	            if(flag==3)
    	                return;
    	        }
    	}
    	public synchronized void compare(){
    		if(Thread.currentThread()==rand&&b==false){
                random=(int)(Math.random()*100)+1; 
                System.out.println("猜数游戏开始!");
                a=true;
                b=true;   
            }
    		if(Thread.currentThread()==rand){
    			if(a==true){
    				try {
    					wait();
    				} catch (InterruptedException e) {
    					
    					e.printStackTrace();
    				}
    			}
    			 if(inputnum<random){  
    	                System.out.println("猜小了!请重猜!");
    	                flag=1;
    	            }
    			 else if(inputnum>random){
    	                System.out.println("猜大了!请重猜!");
    	                flag=2;
    	            }
    			 else if(inputnum==random){
    	                System.out.println("恭喜您猜对了!");
    	                flag=3;
    	            }
    			 a=true;
    	            notifyAll(); 
    			
    		}
    		if(Thread.currentThread()==input){
    			while(a==false){
    				try {
    					wait();
    				} catch (InterruptedException e) {
    					
    					e.printStackTrace();
    				}
    			}
    			 if(flag<3){
                     System.out.println("请输入您猜的数!");
                     Scanner r=new Scanner(System.in);
                     inputnum=r.nextInt();  
                 }
                 a=false;
    		}
    		notifyAll(); 
    	}
    
    }
    

      运行结果

  • 相关阅读:
    系统的访问
    tomcat 和 数据库的连接
    实体类编写规则
    webmagic 爬虫
    docker安装官方Redis镜像并启用密码认证
    解决Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. 问题
    Springboot配置druid报错Failed to bind properties under 'spring.datasource' to javax.sql.DataSource
    阿里云centos7.6搭建SVN远程仓库和Git远程仓库
    java 三大特性封装继承多态
    使用easyui tab需要注意的问题
  • 原文地址:https://www.cnblogs.com/shanshan3/p/12068655.html
Copyright © 2011-2022 走看看