欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

线程基本使用

程序员文章站 2023-02-18 12:28:30
1.继承Thread,覆盖run()方法 public class ThreadDemo1 extends Thread{ @Override public void run() { for (int i = 0; i < 20; i++) { /* System.out.println("run" ......

1.继承thread,覆盖run()方法

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.实现runnable接口,覆盖run()方法

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.线程案例

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()+"正在卖第"+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();
    }
}

存在一票多售和无效票,需要解决线程安全问题

在可能出现线程问题的代码上添加同步锁,保证锁为同一把就完了;新创建一个对象,或者this当前类,或者同步代码块,同步方法;静态方法使用静态变量也有可能出现线程问题,还是得使用同步机制。

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()+"正在卖第"+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()+"正在卖第"+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()+"正在卖第"+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()+"正在卖第"+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()+"正在卖第"+ticket+"张票");
                ticket--;
            }
            lock.unlock();
        }

    }
}