zoukankan      html  css  js  c++  java
  • JAVA第九周作业&实验七

    学习关于多线程的内容:

    自己自学所敲的代码:同步的俩种方法,我这用的是同步代码块的方法!

    package com.itcast.abj.demo01;
    
    public class MyThread implements Runnable {
        private int ticket=1000;
    
        @Override
        public void run() {
            for (int i = 0; i <10000 ; i++) {
                synchronized (this){//同步代码块
                    if(ticket>0){
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("卖票:ticket="+ticket--);
                    }
                }
    
            }
    
        }
    }

    测试:

    package com.itcast.abj.demo01;
    
    public class Demo01 {
        public static void main(String[] args) {
            MyThread myThread=new MyThread();
    
            Thread t1=new Thread(myThread);
            Thread t2=new Thread(myThread);
            Thread t3=new Thread(myThread);
            Thread t4=new Thread(myThread);
            Thread t5=new Thread(myThread);
            Thread t6=new Thread(myThread);
            Thread t7=new Thread(myThread);
            Thread t8=new Thread(myThread);
            Thread t9=new Thread(myThread);
            Thread t10=new Thread(myThread);
    
            t1.start();
            t2.start();
            t3.start();
            t4.start();
            t5.start();
            t6.start();
            t7.start();
            t8.start();
            t9.start();
            t10.start();
        }
  • 相关阅读:
    各种排序
    最大子数组的和与积
    字符串距离
    二叉树的基本操作
    C++11创建线程的几种方式
    二分查找
    汉诺塔问题
    读写锁实现
    全排列
    数字转汉字
  • 原文地址:https://www.cnblogs.com/changanshisanzhao/p/11739516.html
Copyright © 2011-2022 走看看