Basic thread usage

1. Inherit Thread, override run() method

public class ThreadDemo1 extends Thread{
    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
          /*  System.out.println("run"+i);
            Thread thread = Thread.currentThread();
            System.out.println(thread);
            String name = thread.getName();
            System.out.println(name);*/
            System.out.println(Thread.currentThread().getName()+"===>"+i);
        }

    }
}

public class MainTest {
    public static void main(String[] args) {
        ThreadDemo1 threadDemo1 = new ThreadDemo1();
        ThreadDemo1 threadDemo11 = new ThreadDemo1();
        threadDemo1.start();
        threadDemo11.start();
        for (int i = 0; i < 20; i++) {
            System.out.println("main"+i);
        }
    }
}

2. Implement the Runnable interface and override the run () method

public class RunnableImpl implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            System.out.println(Thread.currentThread().getName()+"=====>"+i);
        }
    }
}

public class MainTest {
    public static void main(String[] args) {
        RunnableImpl run = new RunnableImpl();
        new Thread(run).start();
        new Thread(run).start();
        new Thread(run).start();
        new Thread(run).start();
        for (int i = 0; i < 20; i++) {
            System.out.println("main"+i);
        }
    }
}

3. Thread case

public class RunnableImpl implements Runnable {
    private int ticket=100;
    @Override
    public void run() {
        while (true){
            if (ticket>0){
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"Selling first"+ticket+"Zhang ticket");
                ticket--;
            }
        }

    }
}

public class TicketDemo {
    public static void main(String[] args) {
        RunnableImpl runnable = new RunnableImpl();
        Thread t1 = new Thread(runnable);
        Thread t2 = new Thread(runnable);
        Thread t3 = new Thread(runnable);
        t1.start();
        t2.start();
        t3.start();
    }
}

There is one ticket for oversold and invalid tickets, and thread safety issues need to be solved

Add a synchronization lock to the code that may have thread problems to ensure that the lock is the same one. Create a new object, or this current class, or synchronization code block, or synchronization method. Using static variables in static methods may also cause thread problems. You still need to use the synchronization mechanism.

public class RunnableImpl implements Runnable {
    private int ticket=100;
    Object object=new Object();
    @Override
    public void run() {
        while (true){
          synchronized (object){
              if (ticket>0){
                  try {
                      Thread.sleep(10);
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
                  System.out.println(Thread.currentThread().getName()+"Selling first"+ticket+"Zhang ticket");
                  ticket--;
              }
          }
        }

    }
}
public class RunnableImpl implements Runnable {
    private int ticket=100;
    @Override
    public void run() {
        while (true){
            payTicket();

        }

    }
    public synchronized void payTicket(){
        if (ticket>0){
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"Selling first"+ticket+"Zhang ticket");
            ticket--;
        }
    }
}
public class RunnableImpl implements Runnable {
    private int ticket=100;
    @Override
    public void run() {
        while (true){
           synchronized (this){
               if (ticket>0){
                   try {
                       Thread.sleep(10);
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
                   System.out.println(Thread.currentThread().getName()+"Selling first"+ticket+"Zhang ticket");
                   ticket--;
               }
           }
        }

    }
}
public class RunnableImpl implements Runnable {
    private static int ticket=100;
    @Override
    public void run() {
        while (true){
            payTicket();

        }

    }
    public static synchronized void payTicket(){
        if (ticket>0){
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"Selling first"+ticket+"Zhang ticket");
            ticket--;
        }
    }
}
public class RunnableImpl implements Runnable {
    private int ticket=100;
    Lock lock=new ReentrantLock();
    @Override
    public void run() {
        while (true){
            lock.lock();
            if (ticket>0){
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"Selling first"+ticket+"Zhang ticket");
                ticket--;
            }
            lock.unlock();
        }

    }
}

Tags: Java

Posted on Mon, 23 Mar 2020 09:05:33 -0700 by sidsel