Java Basic Threading Operations (with code)

Source: Internet
Author: User

Cheer up, clean up the contents of Java concurrency. Start is a basic threading operation

Thread State switching:

New Thread:

  

@Test      Public void Newtread () {        new Thread (new  Runnable () {                        @Override            public  void  run () {                System.out.println ("OK ...");            }        );        T1.start ();                    }

To terminate a thread:

Thread.stop () is not recommended for use. It will release all monitor

Break thread in:

public void Thread.Interrupt ()//Break thread in
public boolean thread.isinterrupted ()//Determine if interrupted
public static Boolean thread.interrupted ()//Determines if the interrupt is interrupted and clears the current interrupt state

 Public void run () {while (true) {if(Thread.CurrentThread (). isinterrupted ()) { System.out.println ("interruted!" );  Break ;} Thread.yield ();}} T1.start (); T1.interrupt ();

Sleep

public static native void sleep (long Millis) throws Interruptedexception

  

 Public voidrun () { while(true){if(Thread.CurrentThread (). isinterrupted ()) {System.out.println ("Interruted!"); Break;}Try{Thread.Sleep (2000);} Catch(interruptedexception e) {System.out.println ("Interruted when Sleep");//sets the interrupt state, which clears the interrupt mark bit after an exception is thrownThread.CurrentThread (). interrupt ();} Thread.yield ();}}

Suspend and Continue execution:

–suspend () does not release the lock
– If the lock occurs before the resume (), the deadlock occurs

Waiting for thread to end (join) and humility (yeild)

Public final void Join () throws Interruptedexception
Public final synchronized void join (long Millis) throws Interruptedexception

The nature of Join
while (IsAlive ()) {
Wait (0);
}

When the thread finishes executing, the system calls Notifyall ();

  

 Public classJoinmain { Public volatile Static intI=0; Public Static classAddthreadextendsthread{@Override Public voidrun () { for(i=0;i<10000000;i++);}} Public Static voidMain (string[] args)throwsinterruptedexception {addthread at=NewAddthread (); At.start (); At.join (); System.out.println (i);}}

Synchronized
– Specifies the Lock object: Locks the given object and obtains a lock on the given object before entering the synchronization code.
– Acts directly on the instance method: locks the current instance and obtains the current instance's lock before entering the synchronization code.
– Acts directly on a static method: It is equivalent to locking the current class and getting the lock of the current class before entering the synchronization code.
? Object.wait () obejct.notify ()

  

Java Basic Threading Operations (with code)

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.