多线程2
Posted 黑土白云
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多线程2相关的知识,希望对你有一定的参考价值。
1.
1 public class MultiThread { 2 private static int num = 0; 3 4 public static synchronized void printNum(String tag){ 5 if("a".equals(tag)){ 6 System.out.println("tag a, set num over"); 7 num = 100; 8 } else { 9 System.out.println("tag b, set num over"); 10 num = 200; 11 } 12 System.out.println("tag:"+tag+",num=" + num); 13 } 14 15 public static void main(String[] args) { 16 final MultiThread multiThread1 = new MultiThread(); 17 final MultiThread multiThread2 = new MultiThread(); 18 19 Thread t1 = new Thread(new Runnable() { 20 @Override 21 public void run() { 22 multiThread1.printNum("a"); 23 } 24 }); 25 26 Thread t2 = new Thread(new Runnable() { 27 @Override 28 public void run() { 29 multiThread2.printNum("b"); 30 } 31 }); 32 33 t1.start(); 34 t2.start(); 35 36 /* 37 * tag a, set num over 38 tag:a,num=100 39 tag b, set num over 40 tag:b,num=200 41 */ 42 } 43 }
以上是关于多线程2的主要内容,如果未能解决你的问题,请参考以下文章