多进程就是指计算机同时执行多个进程,一般是同时运行多个软件。
//多个线程操作同一个对象 public class ThreadDemo3 implements Runnable{ private int tickNums=10; boolean flag=true; public void run() { while(flag){ try{ Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } buy(); } } private synchronized void buy() {//加上了synchronized关键字 if(tickNums<=0){ flag=false; return; } System.out.println(Thread.currentThread().getName()+"拿到了第"+tickNums--+"票"); } public static void main(String[] args) { ThreadDemo3 threadDemo3=new ThreadDemo3(); new Thread(threadDemo3,"小明").start(); new Thread(threadDemo3,"小红").start(); new Thread(threadDemo3,"小蓝").start(); } }
https://www.cnblogs.com/OfflineBoy/p/14613402.html
守护线程实现方法: