1. Thread state

package com.cjg.xiancheng;
public class threadStop implements Runnable   {
    // If Boolean is left blank, the default value is flase
    private  boolean flag=true;
    @Override
    public void run(a) {
        int i =1;
       while (flag){
           System.out.println("* * * *"+i++); }}public void stop(a){
        this.flag=false;
    }
    public static void main(String[] args) {
        threadStop threadStop = new threadStop();
        new Thread(threadStop,"me").start();
        for (int i = 0; i < 1000; i++) {
            System.out.println("main"+i);
            if (i==500){
                threadStop.stop();
                System.out.println("stop============================================================================="); }}}}Copy the code
package com.cjg.xiancheng;
import java.text.SimpleDateFormat;
import java.util.Date;
public class threadTime {
    public static void main(String[] args) throws InterruptedException {
        f();
        while (true) {// Prints the current system time
            Date date = new Date(System.currentTimeMillis());
            Thread.sleep(1000);
            System.out.println(new SimpleDateFormat("yyyy--MM--dd HH:mm:ss").format(date)); }}public static void f(a) throws InterruptedException {
        int i=10;
        while (true){
            Thread.sleep(100);
            System.out.println(i--);
            if (i==0)break; }}}Copy the code

2. Comity of threads

package com.cjg.xiancheng;
public class threadYield {
    public static void main(String[] args) {
        didi didi = new didi();
        new Thread(didi,"Brother Number one").start(); new Thread(didi,"Brother Number two").start(); }}class didi implements Runnable{
    @Override
    public void run(a) {
        System.out.println(Thread.currentThread().getName()+"start");
        Thread.yield();
        System.out.println(Thread.currentThread().getName()+"end"); }}Copy the code
Join

◆Join Join thread, wait for this thread to complete, then execute other threads, other threads block

package com.cjg.xiancheng;
public class threadJoin implements  Runnable {
    @Override
    public void run(a) {
        for (int i = 0; i < 1000; i++) { System.out.println(Thread.currentThread().getName()+i); }}public static void main(String[] args) throws InterruptedException {
        threadJoin threadJoin = new threadJoin();
        Thread thread = new Thread(threadJoin, "vip");
                thread.start(); // The thread has started
        for (int i = 0; i < 500; i++) {
            System.out.println(Thread.currentThread().getName()+i);
            if (i==100) {Only the join thread is executed until the join thread terminatesthread.join(); }}}}Copy the code

3. Check the status of the thread

package com.cjg.xiancheng;
public class threadStatus {
    public static void main(String[] args) throws InterruptedException {
        Thread thread =new Thread(()->{
            for (int i = 0; i <5 ; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =");
        });
        Thread.State state = thread.getState();
        System.out.println(state);
        thread.start();
        while(state ! =Thread.State.TERMINATED){ Thread.sleep(100); state = thread.getState(); System.out.println(state); }}}Copy the code

4. Thread priority

A low priority simply means a low probability of getting a schedule. It’s not like a low priority doesn’t get called. It all depends on CPU scheduling

Performance inversion is possible

package com.cjg.xiancheng;
public class threadMore {
    public static void main(String[] args) {
        System.out.println(Thread.currentThread().getName()+"The"+Thread.currentThread().getPriority());
        more more = new more();
        Thread thread = new Thread(more);  Thread thread1 = new Thread(more);  Thread thread2 = new Thread(more);
        thread.setPriority(Thread.MAX_PRIORITY);
        thread1.setPriority(4); thread2.setPriority(Thread.MIN_PRIORITY); thread.start(); thread1.start(); thread2.start(); }}class more implements Runnable{
    @Override
    public void run(a) {
        System.out.println(Thread.currentThread().getName()+"The"+Thread.currentThread().getPriority()); }}Copy the code

5. Thread daemon

◆ Threads are divided into user threads and daemons. ◆ The VM must ensure that the user threads finish executing. ◆ The VM does not need to wait for the daemons to finish executing.

package com.cjg.xiancheng;
public class threadDaemon {
    public static void main(String[] args) {
        me me = new me();
        parents parents = new parents();
        // Set the daemon thread
        Thread thread = new Thread(me);
        thread.setDaemon(true);
        thread.start();
        newThread(parents).start(); }}class me implements Runnable{
    @Override
    public void run(a) {
        while (true){
            System.out.println("I'm watching over you."); }}}class parents implements Runnable{
    @Override
    public void run(a) {
        for (int i = 0; i <= 36500; i++) {
            System.out.println("Mom and Dad are happy."+i);
        }
        System.out.println("happy end"); }}Copy the code

6. Thread synchronization and concurrency

Queue locking ensures thread safety corresponding to rollback of SQL database

In pieces due to the same process of multiple threads share the same piece of storage space, in bring convenient at the same time, also brought access conflict, in order to ensure the correctness of the data to be accessed in the method, when access to join locking mechanism synchronized {when – a thread object exclusive locks, exclusive resources, other threads must wait, Release the lock after use. The following problems exist: one thread holding the lock will cause all other threads requiring the lock to suspend; ◆ In multi-threaded competition, lock, lock release will lead to more context switching and scheduling delay, causing performance problems; ◆ If a thread with a higher priority waits for a thread with a lower priority to release the lock, priority inversion may occur, causing performance problems.

== Each thread interacts with its own working memory, and improper memory control can cause data inconsistency ==

package com.cjg.xiancheng;
public class syn {
    public static void main(String[] args) {
        buy buy = new buy();
        new Thread(buy,"11111").start();  new Thread(buy,"2222222222").start();  new Thread(buy,"33333333333").start(); }}class  buy implements  Runnable{
    private boolean flag=true;
    private int tiket = 10;
    @Override
    public void run(a) {
        while (flag){
            try {
                buyTikets();
            } catch(InterruptedException e) { e.printStackTrace(); }}}private synchronized void buyTikets(a) throws InterruptedException {
        if (tiket<=0){
            flag=false;
            return;
        }
        System.out.println(Thread.currentThread().getName()+"Get the mark"+tiket--+"Ticket");
        Thread.sleep(100); }}// If this is not locked, use a block instead. Pay attention to who is being called and who needs to be added or deleted
synchronizedObject obj {}Copy the code