Seventh and ninth week of test report summary

Analog train station ticket program is completed.

要求:
(1)总票数1000张
(2)10个窗口同时开始卖票;
(3)卖票过程延时1秒钟
(4)不能出现一票多卖或卖出负数号票的情
况。

Code:

package Text10;

public class MyThread implements Runnable{
    
        private int ticket=1000;
        
        public void run() {
             while(true) {
                    synchronized(this){
        
                if(ticket>=0) {
                    try {
                        Thread.sleep(1000);  //延迟一秒
                    }catch(InterruptedException e) {
                        e.printStackTrace();
                        }
    System.out.println(Thread.currentThread().getName()+"卖出一张票,剩下票数:ticket="+ticket--);

                }
            }
        }
    }
}

Test category

package Text10;

public class Text1024 {

    public static void main(String[] args) {
    
                MyThread mt=new MyThread();
                new Thread(mt, "窗口1").start();
                new Thread(mt, "窗口2").start();
                new Thread(mt, "窗口3").start();
                new Thread(mt, "窗口4").start();
                new Thread(mt, "窗口5").start();
                new Thread(mt, "窗口6").start();
                new Thread(mt, "窗口7").start();
                new Thread(mt, "窗口8").start();
                new Thread(mt, "窗口9").start();
                new Thread(mt, "窗口10").start();
 }
    }

Attachment:

to sum up:

1. This week learn the difference between processes and threads;

To achieve multi-threaded area:

Thread class inheritance:

Implement Runnable:

2. learn the common thread of method of operation, they need to remember in practice

3. Learn the synchronization and deadlock:

使用同步代码块:synchronized(同步对象){
                                                                需要同步代码;
                                                                }
和同步方法:
synchronized方法返回值 方法名称(参数列表){}

4. File class and learned RandomAccessFile class, at present, still do not understand, continue learning. Results of operations run feel wrong do not know what went wrong

Guess you like

Origin www.cnblogs.com/saury/p/11735840.html