java thread mechanism found little

最近发现了一个有意思的线程区分
public class MyThreadClass {
public static void main(String[] args) {
final MyThreadClass my=new MyThreadClass();
Thread thread1=new Thread(new Runnable() {
@Override
public void run() {
try {
my.test1();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},“test1”);
Thread thread2=new Thread(new Runnable() {
@Override
public void run() {
try {
my.test2();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},“test2”);
thread1.start();
thread2.start();
}

public void test1() throws InterruptedException{
	synchronized (this) {
		int i=0;
		while(i++<=5){
			System.out.println(Thread.currentThread().getName()+":"+i);
			Thread.sleep(500);
		}
		wait(1000);
		while(i++<10)
		{
			System.out.println(Thread.currentThread().getName()+":"+i);
		}
	}
}

public void test2() throws InterruptedException{
	synchronized(this)
	{
		int i=0;
		while(i++<=5){
			System.out.println(Thread.currentThread().getName()+":"+i);
			Thread.sleep(500);
		}
		wait(1000);
		while(i++<10)
		{
			System.out.println(Thread.currentThread().getName()+":"+i);
		}
	}
}

}
In this case one thread to execution thread, assume thread1 program execution is stopped to wait before executing, then execution Thread2, execution stops to wait, and then continue the unfinished part thread1, executed after executing the Thread2 thread1 section, the output is not completed as follows
test2:. 1
test2: 2
test2:. 3
test2:. 4
test2:. 5
test2:. 6
test1:. 1
test1: 2
test1:. 3
test1:. 4
test1:. 5
test1:. 6
test2:. 8
test2:. 9
test2: 10
test1:. 8
test1:. 9
test1: 10

If the definition of the function into an anonymous class, seemingly equivalent modify, at this time becomes a function of thread1 thread2 operated alternately,
public class MyThreadClass {
public static void main (String [] args) {
Final MyThreadClass = new new MyThreadClass My ();
the Thread Thread1 the Thread new new = (the Runnable new new () {
@Override
public void RUN () {
the try {
the synchronized (the this) {
int I = 0;
the while (I ++ <=. 5) {
the System.out. the println (Thread.currentThread () getName () + ":." + I);
the Thread.sleep (500);
}
the wait (1000);
the while (I ++ <10)
{
System.out.println (Thread.currentThread () .getName () + ":" + I);
}
}
} the catch (InterruptedException E) {
e.printStackTrace ();
}
}
},“test1”);
Thread thread2=new Thread(new Runnable() {
@Override
public void run() {
try {
synchronized(this)
{
int i=0;
while(i++<=5){
System.out.println(Thread.currentThread().getName()+":"+i);
Thread.sleep(500);
}
wait(1000);
while(i++<10)
{
System.out.println(Thread.currentThread().getName()+":"+i);
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},“test2”);
thread1.start();
thread2.start();
}
}
运行结果如下
test2:1
test1:1
test1:2
test2: 2
test1:. 3
test2:. 3
test2:. 4
test1:. 4
test1:. 5
test2:. 5
test1:. 6
test2:. 6
test1:. 8
test2:. 8
test1:. 9
test2:. 9
test1: 10
test2: 10
The reason is simple, when not join thread1 and thread2 locks is the main thread, the main thread after this time thread1 to lock thread2 not run, and the second method is locked thread1 with thread2 two separate threads, plus at this time thread1 after the lock thread2 still running.

Published 17 original articles · won praise 7 · views 2997

Guess you like

Origin blog.csdn.net/znevegiveup1/article/details/84838967