Dark Horse programmer-Java basics-Multithreading

Source: Internet
Author: User

Thread:Is an independent control unit in a process. The thread is controlling the execution of the process.

Thread Creation

1. inherit from the Thread class. Steps:

1. Define class to inherit from thread.

2. Rewrite the run method in the Thread class. Purpose: To customizeCodeStored in the run method. Let the thread run.

Class demoExtends thread
{
Public void run ()
{
For (INT x = 0; x <60; X ++)
System. Out. println ("demo run" + x );

}

}

The running status of the thread:

Ii. method 2 for thread creation: implement the runable Interface

Steps:

1. Define the class to implement the runnable interface

2. overwrite the run method in the runnable interface. Store the code to be run by the thread in the run method.

3. Create a thread object through the Thread class.

4. Pass the subclass object of the runnable interface to the constructor of the thread class as an actual parameter.

5. Call the start method of the thread class to enable the thread and call the run method of the runnable interface subclass.

Class ticketImplements runnable// Extends thread {

Private int tick= 100;

Public voidRun (){

While (true ){

If (tick> 0)

System. Out. println (thread. currentthread (). getname () + "... sale:" + tick ");
Test t2 = new test ("Two ++ ");
T1.start ();
T2.start ();

}

}

Multi-thread security problems: through analysis, it is found that the 0,-1,-2 and other incorrect tickets are printed. Cause: when multiple statements operate on the data shared by the same thread, one thread only executes a part of the multiple statements and has not yet executed

When the row is finished, another thread participates in the execution. Data sharing error. Solution: the statement that shares data for multiple operations can only be executed by one thread. Other threads cannot participate in execution. This is coming out.

Synchronous Code Block

Synchronized (object ){

Code to be synchronized

}

Objects are like locks. The thread holding the lock can be executed in synchronization. The thread that does not hold the lock cannot even obtain the execution right of the CPU, because the lock is not obtained.

Synchronization prerequisites: 1. There must be two or more threads; 2. Multiple Threads must use the same lock.

Benefits: Solves the security problem of multithreading.

Disadvantages: Multiple threads need to judge the lock, which consumes more resources.

Class ticketImplements runnable{

Private int tick= 1000;

Object OBJ = new object ();

Public void run (){

While (true ){

Synchronized (OBJ){

If (tick> 0 ){

// Try {thread. Sleep (10);} catch (exception e ){}

System. Out. println (thread. currentthread (). getname () + "... sale:" + tick );

}

}

}

}

Else

While (true)

Show ();

}

Public synchronized void show () // This

{

If (tick> 0 ){

Try {thread. Sleep (10);} catch (exception e ){}

System. Out. println (thread. currentthread (). getname () + "... show...:" + tick );

}

}

}

}

Else

While (true)

Show ();

}

Public static synchronized void show (){

If (tick> 0 ){

Try {thread. Sleep (10);} catch (exception e ){}

System. Out. println (thread. currentthread (). getname () + "... show...:" + tick>;

S = new single ();

}

}

Return S;

}

}

Class singledemo {

Public static void main (string [] ARGs ){

System. Out. println ("Hello world! ");

}

}

Deadlock:

Class test implements runnable {

Private Boolean flag;

Test (Boolean flag ){

This. Flag = flag;

}

Public void run (){

If (FLAG ){

While (true ){

Synchronized (mylock. Locka ){

System. Out. println (thread. currentthread (). getname () + "... if Locka ");

Synchronized (mylock. lockb ){

System. Out. println (thread. currentthread (). getname () + ".. If lockb ");

}

}

}

}

Else {

While (true ){

Synchronized (mylock. lockb ){

System. Out. println (thread. currentthread (). getname () + "... else lockb ");

Synchronized (mylock. Locka ){

System. Out. println (thread. currentthread (). getname () + "... else Locka ");

}

}

}

}

}

}

Class mylock {

Static object Locka = new object ();

Static object lockb = new object ();

}

Class deadlocktest {

Public static void main (string [] ARGs ){

Thread T1 = new thread (new test (true ));

Thread t2 = new thread (new test (false ));

T1.start ();

T2.start ();

}

}

 

Related Article

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.