zoukankan      html  css  js  c++  java
  • 实验报告(七)及第九周总结

     实验代码:

    package demo2;
    
    public class SaleTicker implements Runnable {
    	public int total=1000;		
    	public int count=0;			
    	@Override
    	public void run() {		
    		while(total>0){
    			synchronized(this){		
    				if(total>0){
    					try {
    						Thread.sleep(1000);	
    					} catch (InterruptedException e) {
    						e.printStackTrace();
    					}
    					count++;		
    					total--;		
    					System.out.println(Thread.currentThread().getName()+"	当前票号:"+count);
    				}
    			}
    		}
    	}
    
    }package demo2;
    
    public class SaleTicker implements Runnable {
    	public int total=1000;		
    	public int count=0;			
    	@Override
    	public void run() {		
    		while(total>0){
    			synchronized(this){		
    				if(total>0){
    					try {
    						Thread.sleep(1000);	
    					} catch (InterruptedException e) {
    						e.printStackTrace();
    					}
    					count++;		
    					total--;		
    					System.out.println(Thread.currentThread().getName()+"	当前票号:"+count);
    				}
    			}
    		}
    	}
    
    }
    package demo2;
    
    public class Test {
    	public static void main(String[] args) {
    		
    		SaleTicker st=new SaleTicker();
    		
    		for(int i=1;i<=10;i++){
    			new Thread(st,"售票点"+i).start();;
    		}
    	}
    }
    

      实验代码截图:

     

     实验结果截图:

     第九周总结:

    本周主要学习了多线程和java的输入输出;

    多线程的实现主要有两种:

    1.继承thread类;

    2实现runnable接口;

    start()是多线程的启动方法;

    run()是多线程的主体;

    两种的区别:

    thread不能资源共享;

    runnable能实现资源共享;

     java的输入输出:

     Flile的主要用法和常量:

  • 相关阅读:
    VC 透明滑动控件Slider Control
    VC 对话框背景颜色、控件颜色
    VS2008下直接安装使用Boost库1.46.1版本
    强大的CImage类
    用CImage类来显示PNG、JPG等图片
    VS2008+Qt 项目目录
    java 中几种常用数据结构
    数据结构与算法——常用数据结构及其Java实现
    C++常用数据结构的实现
    基础数据结构和算法概念
  • 原文地址:https://www.cnblogs.com/hhwcg/p/11728035.html
Copyright © 2011-2022 走看看