Java multithreading

Z, and 2022-05-22 11:39:34 阅读数:767

javamultithreading

java Multithreading

To learn java Multithreading mechanism .
Multithreading : There are multiple execution paths in the program , Can perform multiple tasks .

Realization way

1、 Create a class inheritance Thread class

// Multithreaded class 
public class Demo_3 extends Thread{

// Nonparametric construction method 
public Demo_3(){
}
// A parameterized constructor can name a thread 
public Demo_3(String name){

super(name);
}
@Override
public void run() {

for(int i = 0;i < 100;i ++){

System.out.println(getName() + ":" + i );//getName() Get the thread name 
}
System.out.println(getName() + " completion of enforcement ");
}
}
// Main function test class 
public class Test {

public static void main(String[] args) {

// Create two threads t1,t2
// Call the parameterless construction thread name, default 
//Demo_3 t1 = new Demo_3(" Threads 1");
//Demo_3 t2 = new Demo_3(" Threads 2");
// Call the parameterized construct to name the thread 
Demo_3 t1 = new Demo_3(" Threads 1");
Demo_3 t2 = new Demo_3(" Threads 2");
// Start thread 
t1.start();
t2.start();
}
}

2、 Create a class implementation Runnable Interface and override run Method

// Multithreaded class 
public class Demo_3 implements Runnable{

@Override
public void run() {

for(int i = 0;i < 100;i ++){

System.out.println(Thread.currentThread().getName() + ":" + i );//Thread.currentThread().getName() Get the thread name 
}
System.out.println(Thread.currentThread().getName() + " completion of enforcement ");
}
}
// Main function test class 
public class Test {

public static void main(String[] args) {

// Create thread Demo_3 Class object 
Demo_3 demo_3 = new Demo_3();
// establish Thread Class object ,Demo_3 Object as parameter , Get two threads t1,t2
//Thread t1 = new Thread(demo_3);
//Thread t2 = new Thread(demo_3);
// You can also name the thread 
Thread t1 = new Thread(demo_3," Threads 1");
Thread t2 = new Thread(demo_3," Threads 2");
// Start thread 
t1.start();
t2.start();
}
}

Thread priority

The execution of threads is random , But its priority can also be changed ( Execution probability ).

// Get thread priority 
int i = t1.getPriority();
int j = t2.getPriority();
System.out.println(i);
System.out.println(j);
i:5
j:5
Default 5
// Set thread priority ( It can interfere with the sequence of thread execution )
//public final static int MAX_PRIORITY = 10; Maximum 
//public final static int MIN_PRIORITY = 1; Minimum 
//public final static int NORM_PRIORITY = 5; Default 
t1.setPriority(9);
t2.setPriority(2);

Thread control

Thread.sleep( Number of milliseconds );// The executing thread pauses for the specified number of milliseconds 
t1.join();// wait for t1 Thread end 
//void setDeamon(boolean b);// This thread is the daemon thread of the main thread , When the main thread ends , The daemon thread ends .
// Set the main thread 
Thread.currentThread().setName(" The main thread ");
// Setting up the guardian thread 
t1.setDeamon(true);
t2.setDeamon(true);

Thread synchronization protects shared data

Thread synchronization : When a thread executes a piece of code , Another thread waits for this thread to finish executing before executing .
Thread asynchronous : When a thread executes a piece of code , Another thread can also execute this code .

1、 Synchronize code blocks to lock shared data

//synchronized ( Any object ( The same object )){ Code block } // At this time, other threads cannot execute this code 

2、 The synchronization method locks the shared data

//private synchronized void t(){}// Common methods lock objects :this
//private static synchronized void t(){}// Static method lock object : Class name .class

Lock lock ( Interface ) Protect shared data

//lock();// Gets the lock 
//unlock();// Release the lock 
// establish Lock lock 
Lock lock = new ReentrantLock();
// Set on try-finally Ensure lock release 
try {

lock.lock();
}finally {

lock.unlock();
}
copyright:author[Z, and],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/142/202205211746506803.html