zoukankan      html  css  js  c++  java
  • 子程序循环10次,接着主程序循环100次,然后子程序又循环10次,主程序循环100次,这样循环50次

    //子程序循环10次,接着主程序循环100次,然后子程序又循环10次,主程序循环100次,这样循环50次。
    public class ThreadTest 
    {
    	public static void main(String[] args) 
    	{
    		MyThread mythread = new MyThread();
    		new Thread(new Runnable()//传递给Thread的是一个实现了Runnable接口的匿名内部类的对象的引用
    		{
    			@Override
    			public void run() 
    			{
    				for(int i = 1; i <= 50; i ++)
    				{
    					mythread.subThread(i);
    				}
    			}
    		}).start();
    		
    		new Thread(new Runnable()
    		{
    			@Override
    			public void run() 
    			{
    				for(int i = 1; i <= 50; i ++)
    				{
    					mythread.mainThread(i);
    				}
    			}
    		}).start();
    	}
    }
    
    class MyThread
    {
    	public synchronized void subThread(int j)
    	{
    		for(int i = 1; i <= 10; i ++)
    		{
    			System.out.println("子线程"+j+":循环"+i+"次");
    		}
    		notifyAll();
    		try {
    			if(j!=50)
    			{
    				wait();
    			}
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    	}
    	
    	public synchronized void mainThread(int j)
    	{
    		for(int i = 1; i <= 100; i ++)
    		{
    			System.out.println("主线程"+j+":循环"+i+"次");
    		}
    		notifyAll();
    		try {
    			if(j!=50)
    			{
    				wait();
    			}
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    	}
    }
    
  • 相关阅读:
    ROS tf 两个常用的函数
    C/C++ assert()函数用法总结
    drand48 等 随机数生成函数
    PF部分代码解读
    Error "Client wants topic A to have B, but our version has C. Dropping connection."
    launch 文件的写法
    Spring七大框架
    web.xml filter配置
    web.xml listener配置
    web.xml加载过程
  • 原文地址:https://www.cnblogs.com/masterlibin/p/5614686.html
Copyright © 2011-2022 走看看