Runnable接口实现多线程(共享资源)

package com.cyj.thread.create;

public class Cuiyongyuan implements Runnable{
	private int num = 0;
	
	public void run() {
		while(true) {
			if(num>=500) {
				break;
			}
			
			num += 1;
			if(num%2 == 0) {
			    System.out.println(Thread.currentThread().getName() + "评论支持了崔永元一下");
			}else {
				System.out.println(Thread.currentThread().getName() + "评论损了冯裤子一下");
			}
			
		}
	}
  
	public static void main(String[] args) {
		
		//真实角色
		Cuiyongyuan cyy = new Cuiyongyuan();
		
		//代理角色
		Thread c1 = new Thread(cyy,"一号支持者");
		Thread c2 = new Thread(cyy,"二号支持者");
		Thread c3 = new Thread(cyy,"三号支持者");
		
		//启动线程
		c1.start();
		c2.start();
		c3.start();
	}
	
}

猜你喜欢

转载自blog.csdn.net/qq_42036616/article/details/81060550