Java thread Problems

Source: Internet
Author: User

The title name may be a bit confusing. It seems that I am talking about some profound questions. In fact, I mainly talk about several methods. As for the accuracy of the content I'm talking about, it is hard for me to ensure that, after all, I am also a cainiao and hope that I will not mislead others. However, because I am a cainiao, what I am talking about may be easy to understand, making it also a cainiao bird, suddenly open, good, not much nonsense.


Let's talk about two methods: stop, suspendtwo methods. One is to stop the thread, and the other is to suspend the thread. But they have something in common. They do not release the object lock, which can easily lead to a deadlock. Why?


When you are executing the run method of this thread, after you use the suspend or stop method, the thread is suspended or stopped for other threads to run, however, if the thread that gets the cpu is in a synchronous relationship with the thread that you just suspended or stopped, that is, if the same object lock is used, the thread cannot run, because the lock is not released after the thread has been suspended or stopped, there are two methods to release the lock, one is the completion of the sychronized statement, in the sychronized statement, the wait method of the object lock is called to release the object lock. Obviously, the stop or suspend method cannot meet any of the above two requirements, so the object lock will not be released, the Synchronization Code enters a deadlock state. Therefore, as early as jdk1.2.


The following describes the differences between sleep and wait. This is important. I think there may be a lot of interviews. I also checked on the Internet, and they thought they were all obscure. It was difficult for people of the cainiao level, and I did not dare to say that I knew it very well, however, I try to use my own words to make it difficult for everyone to see.


First, sleep is the thread method, and wait is the object method. The most important thing is that after sleep is called, the running process enters the sleep state, giving the cpu to other processes, but do not release the object lock. Wait gives way to the cpu, enters the blocking status, and releases the object lock. Another method is wake up, sleep uses the interrupt method, and wait uses the y method. The former is the Thread method and the latter is the Object method.

After waking up, the system enters the running status and waits for the call.


The following code helps you better understand the differences between wait and sleep and hopes to help you.


Public class MultiThread {

Public static void main (String [] args ){

New Thread (new Thread1 (). start ();

Try {

Thread. sleep (10 );

} Catch (InterruptedException e ){

E. printStackTrace ();

}

New Thread (new Thread2 (). start ();

}

Private static class Thread1 implements Runnable {

Public void run (){

Synchronized (MultiThread. class ){

System. out. println ("enter thread1 ...");

System. out. println ("thread is waiting ");

Try {

MultiThread. class. wait ();

// Wait releases the object lock, so Thread2 can run. If it is changed to Thread. sleep, Thread2 cannot run.

} Catch (InterruptedException e ){

// TODO Auto-generated catch block

E. printStackTrace ();

}

System. out. println ("thread1 is going on ..");

System. out. println ("thread1 is being over! ");

}

}

}

Private static class Thread2 implements Runnable {

Public void run (){

Synchronized (MultiThread. class ){

System. out. println ("enter thread2 ...");

System. out. println ("thread2 running y other can release wait status ..");

MultiThread. class. Y (); // wake up Thread1 of wait

System. out. println ("thread2 is sleeping ten millisecond ..");

Try {

Thread. sleep (10); // sleep does not release the object lock, so Thread1 cannot run even if it gets the cpu.

} Catch (InterruptedException e ){

E. printStackTrace ();

}

System. out. println ("thread2 is going on ...");

System. out. println ("thread2 is being over! ");

}

}

}

}


The output result is:

Enter thread1...

Thread is waiting

Enter thread2...

Thread2 running y other can release wait status ..

Thread2 is sleeping ten millisecond ..

Thread2 is going on...

Thread2 is being over!

Thread1 is going on ..

Thread1 is being over!


I believe that I will be able to understand the wisdom of my family, and I will not explain it too much.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.