黑马程序员--java基础复习--线程--同步与通信
package thread;public class TestThread {private static int count;int line = 5;public static void main(String[] args) {TestThread tt = new TestThread();tt.startThread();tt.startThread();}class CountThread implements Runnable {public void run() {while (count < 25) {//synchronized (TestThread.class) {//if(count < 25){count++;System.out.print(Thread.currentThread().getName() + "--" + count + "\t");line--;if (line == 0) {System.out.println();line = 5;}}//}//}}}public void startThread() {new Thread(new CountThread()).start();}}
package thread;public class TestThread {private static int count;int line = 5;public static void main(String[] args) {TestThread tt = new TestThread();tt.startThread();tt.startThread();}class CountThread implements Runnable {public void run() {while (count < 25) {synchronized (TestThread.class) {if(count < 25){count++;System.out.print(Thread.currentThread().getName() + "--" + count + "\t");line--;if (line == 0) {System.out.println();line = 5;}}}}}}public void startThread() {new Thread(new CountThread()).start();}}
?
当然这是最背的情况,实际情况更复杂,需要用配合代码来说明的。
?
----------------------?ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------
详细请查看:http://edu.csdn.net