多线程

实现一个多线程的方法:

方式一:

  1. 创建MyThread类继承thread类
  2. 在MyThread类中重写run方法“
  3. 创MyThread类的对象
  4. 启动线程
public class MultiThreading {

    static class MyThread extends Thread{
        @Override
        public void run() {	
            // TODO Auto-generated method stub
            for(int i = 0; i < 100; i++) {
                System.out.println(i);
            }
        }
        
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        MyThread m1 = new MyThread();
        MyThread m2 = new MyThread();
        m2.start();
        m1.start();

    }

}

设置线程名称:

public class MyThread {

    static class MyThread extends Thread{
        public MyThread(String name) {
            super(name);
        }
        @Override
        public void run() {	
            // TODO Auto-generated method stub
            for(int i = 0; i < 100; i++) {
                System.out.println(i + "___" + getName());
            }
        }
        
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
         //使用构造方法设置线程名称
         //需要写构造方法将接受到的那么指向父类name
         MyThread m1 = new MyThread("线程1");
         MyThread m2 = new MyThread("线程2");
        // m1.setName("线程1"); 设置线程名称
         m1.start();
         m2.start();
        
       //获取当前执行的线程
       System.out.println(Thread.currentThread().getName());
    }

}

线程调度:

public class MultiThreading  {

    static class MyThread extends Thread{
        public MyThread(String name) {
            super(name);
        }
        @Override
        public void run() {	
            // TODO Auto-generated method stub
            for(int i = 0; i < 100; i++) {
                System.out.println(i + "___" + getName());
            }
        }
        
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
    
    
        MyThread m1 = new MyThread("线程1");
        MyThread m2 = new MyThread("线程2");
        
        
        //给当前线程设置优先级
        m1.setPriority(10);
        m2.setPriority(1);
        

        //返回当前线程的优先级
        System.out.println(m1.getPriority());
        //优先级默认是5最小是1最大是10
        
        m1.start();
        m2.start();

线程控制:

package SE;

public class 多线程 {

    static class MyThread extends Thread{
        public MyThread(String name) {
            super(name);
        }
        @Override
        public void run() {	
            // TODO Auto-generated method stub
            for(int i = 0; i < 100; i++) {
                System.out.println(i + "___" + getName());
                
                
//				//调用一次就休眠一次
//				try {
//					Thread.sleep(1000);
//				} catch (InterruptedException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
            }
        }
        
    }
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
    
    
        MyThread m1 = new MyThread("线程1");
        MyThread m2 = new MyThread("线程2");
        //暂停线程1000毫秒执行
        Thread.currentThread().setName("主线程");
        
        
        //设置守护进程
        //没有非守护线程了就结束了
        m1.setDaemon(true);
        m2.setDaemon(false);
        m1.start();
        m2.start();
        for(int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName());
        }
    }

}

方式二:

  1. 创建MyRunnable类实现Runnable接口
  2. 在MyRunnable类中重写run方法
  3. 创建MyRunnable对象
  4. 创建Thread对象,把MyRunnable对象当构造方法的参数传入进去
  5. 启动线程

package SE;

public class Mybb implements Runnable{

    @Override
    public void run() {
        // TODO Auto-generated method stub
        for(int i = 0; i < 100; i++){
            System.out.println(i + Thread.currentThread().getName());
        }
    }
}
-------------------------------------------------------------------------------------------
package SE;

public class MyRunnable {

    public static void main(String[] args) throws Exception{
        // TODO Auto-generated method stub
        //创建对象
        Mybb m1 = new Mybb();
        //将m1当对象传入Thread中,设置线程对象
        Thread t1 = new Thread(m1, "线程1");
        Thread t2 = new Thread(m1,"线程2");		
        t1.start();	

        t2.join();
    }

}a

案例:售票

package SE;

public class Mybb implements Runnable{
        
    @Override
    public void run() {
        // TODO Auto-generated method stub
        
        for(int i = 1; i <= 100; i++) {
            
            
            /*
             * 
             * 假如t1线程抢到了cpu执行权输出售票,休眠100毫秒
             * 如果t2线程抢到了cpu执行权输出这时候t2也下在休息100毫秒
             * t3线程抢到了cpu执行权输出这时候t3也下在休息100毫秒
             * t1是最先睡眠的那就是说t1是最先开始执行的
             */
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            
            System.out.println(Thread.currentThread().getName() + "正在出售第" + i + "张票");
            
    
        }
    
    }
}
-------------------------------------------------------------------------------------------------------------------
package SE;

public class MyRunnable {

    public static void main(String[] args) throws Exception{
        // TODO Auto-generated method stub
        Mybb m1 = new Mybb();
        Thread t1 = new Thread(m1,"线程1");
        Thread t2 = new Thread(m1,"线程2");		
        Thread t3 = new Thread(m1,"线程3");

        t1.start();	
        t2.start();
        t3.start();
    }

}

package SE;

public class Mybb implements Runnable{
        int i = 100;
        Object o = new Object();
    @Override
    public void run() {
        // TODO Auto-generated method stub
        while(true) {
                //使用代码块包裹住重复调用的代码
                //synchronized (任意对象) 需要在外部创建对象同一个对象就是用同意一把锁锁住就不会出现同时在出售同一张票
                synchronized (o) {
                    if(i > 0) {
                        try {
                            Thread.sleep(200);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        System.out.println(Thread.currentThread().getName() + "正在出售第" + i + "张票");
                        i--;
                    }
                }
        }
    
    }
}
------------------------------------------------------------------------------------------------------------------------------------------
package SE;

public class MyRunnable {

    public static void main(String[] args) throws Exception{
        // TODO Auto-generated method stub
        Mybb m1 = new Mybb();
        Thread t1 = new Thread(m1,"线程1");
        Thread t2 = new Thread(m1,"线程2");		
        Thread t3 = new Thread(m1,"线程3");

        t1.start();	
        t2.start();
        t3.start();
    }

}

Lock锁:

package SE;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Mybb implements Runnable{
        int i = 100;
        Object o = new Object();		
        //创建Lock锁对象
        Lock lo = new ReentrantLock();
    @Override
    public void run() {
        // TODO Auto-generated method stub

        while(true) {
            //上锁
            try {
                lo.lock();
                if(i > 0) {
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "正在出售第" + i + "张票");
                    i--;
                }
                //释放锁
                lo.unlock();
                
            } finally {   //一定会被执行的
                lo.unlock();
            }
        }
    
    }
}

您可能还喜欢...