First, the test report
Experimental task details:
Analog train station ticket program is completed.
Requirements:
(1) the total number of votes 1000;
(2) simultaneously started ticket window 10;
(3) a second delay procedure ticket;
(4) a case of selling tickets or buy tickets negative numbers can not appear .
class Market implements Runnable { private int ticket; public int getTicket() { return ticket; } void setTicket(int ticket) { this.ticket = ticket; } public void run() { for (int i = 1; i < 100; i++) { synchronized (this) { if (ticket > 0) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } ticket--; System.out.println(Thread.currentThread().getName() + "售票成功;" + "剩余:" + ticket+"张"); } } } } } class seven { public static void main(String[] args){ Thread[] t=new Thread[10]; Market mar=new Market(); mar.setTicket(1000); for(int i=0;i<10;i++){ new Thread(mar,i+1+"号窗口").start(); } } }
Second, summary
Processes and threads
Process: the implementation of a program, that is, once the program is loaded into memory and ready to perform, it is a process. Process is a fundamental concept of resource allocation, but also is the basic unit is scheduled to run, is a unit of concurrent execution of the system.
Thread: implementation of a single process for each task is a thread. A thread is the smallest unit of the operation is performed in the process.
The difference between processes and threads
A thread is the smallest unit of program execution, and the process is the smallest unit of the operating system of allocating resources;
A process of one or more of threads, the thread is a different process routes execution code;
进程之间相互独立,但同一进程下的各个线程之间共享程序的内存空间(包括代码段,数据集,堆等)及一些进程级的资源(如打开文件和信号等),某进程内的线程在其他进程不可见;
调度和切换:线程上下文切换比进程上下文切换要快得多。