Java--多线程

//在学习多线程的过程中要知道每次的线程结果都可能不相同(和CPU的分配有关),要多运行几次找到规律

//关于多个线程操作一个对象和多个线程操作多个对象

–多线程操作一个对象就可以分享一个资源(多线程单个对象容易引发并发问题)
多线程操作多个对象()

1.线程的定义(每个类后面可以加引号,个人习惯不同)
这里写图片描述

这里写图片描述

这里写图片描述


继承Thread方法实现多线程
这里写图片描述

实现Runnable接口实现多线程
这里写图片描述

两种方法的联系
这里写图片描述

两种方法的区别
这里写图片描述
这里写图片描述

start是什么时候调用run()方法的

/**
 * Causes this thread to begin execution; the Java Virtual Machine
 * calls the <code>run</code> method of this thread.
 * <p>
 * The result is that two threads are running concurrently: the
 * current thread (which returns from the call to the
 * <code>start</code> method) and the other thread (which executes its
 * <code>run</code> method).
 * <p>
 * It is never legal to start a thread more than once.
 * In particular, a thread may not be restarted once it has completed
 * execution.
 *
 * @exception  IllegalThreadStateException  if the thread was already
 *               started.
 * @see        #run()
 * @see        #stop()
 */
public synchronized void start()

这个问题有点意思,因为在Thread的start中并没有显式调用run方法,不过研究上面的start这个方法的注释,注意第一行就可以发现jvm会调用thread的run()方法

2.多线程的操作

Thread.currentThread.getname(); 得到当前线程的名字
这里写图片描述

xx.isAlive();判断线程是否存活
这里写图片描述
这里写图片描述

--代码执行之后可能为TRUE也可能为FALSE,如果到该点分线程在main线程之前完成则为FALSE,线程已经完毕

线程的强制运行,执行join方法之后只能等join中的线程运行完毕才能运行其他线程。

               try
                {
                    t1.join();
                }
                catch (InterruptedException e)
                {

                }

线程的休眠
这里写图片描述

线程的终止
这里写图片描述
这里写图片描述

线程Thread_0本应该休眠十秒在执行却在两秒后被主线程interrupt中断。休眠一旦终止就执行catch中的代码。

后台线程

这里写图片描述
这里写图片描述

线程的优先级(priority)

这里写图片描述

这里写图片描述

线程的礼让
这里写图片描述

示例
这里写图片描述
继承thread接口
这里写图片描述
implement runnable实例Runnable接口
这里写图片描述

死锁与同步
这里写图片描述
这里写图片描述

使用同步解决问题
这里写图片描述
这里写图片描述

死锁
这里写图片描述

线程操作实例
这里写图片描述

package cn.edu.wic.DXCsisuo;



class Info{
    private String name = "zjl";
    private String context = "加油,努力";

    public String getName()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public String getContext()
    {
        return context;
    }
    public void setContext(String context)
    {
        this.context = context;
    }   
}

    class producer implements Runnable{
        private Info info = null;
        public producer(Info info){
            this.info=info;
        }
        public void run(){
          boolean flag = false;
          for(int i=0;i<10;i++){

            if(flag){
                this.info.setName("zjl");
                try
                {
                    Thread.sleep(90);
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
                this.info.setContext("加油,努力");
                flag = false;
            }else{
                this.info.setName("我是zjl");
                try
                {
                    Thread.sleep(90);
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
                this.info.setContext("我一定会成功的");
                flag = true;
            }
          }
        }
    }


    class Consumer implements Runnable{
        private Info info = null;
        public Consumer(Info info){
            this.info = info;
        }

        public void run(){
            for(int i=0;i<10;i++){
            try
            {
                Thread.sleep(100);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
            System.out.println(this.info.getName()+"----->"+this.info.getContext());
         }  
        }

    }

public class ScXfTest
{
    /**
     * 
     */
    public static void main(String[] args)
    {
        Info i = new Info();
        producer pr = new producer(i);
        Consumer con = new Consumer(i);

        new Thread(pr).start();
        new Thread(con).start();


    }

}

这里写图片描述

package cn.edu.wic.DXCsisuoTest;

class Info{
    private String name = "zjl";
    private String context = "加油,努力";

    public String getName()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public String getContext()
    {
        return context;
    }
    public void setContext(String context)
    {
        this.context = context;
    }

    public synchronized void set(String name,String context){
        this.setName(name);
        try
        {
            Thread.sleep(30);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        this.setContext(context);

    }
    public synchronized void get(){

        try
        {
            Thread.sleep(30);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        System.out.println(this.getName()+"-->"+this.getContext());
    }
}

class producer implements Runnable{
      private Info info = null;
      public producer(Info info){
          this.info= info;
      }
      public void run(){
          boolean flag = false;
          for(int i=0;i<20;i++){
              if(flag){
                this.info.set("zjl", "加油努力");
                  flag= false;
              }else {
                this.info.set("zjl", "我一定会成功的");
                      flag=true;
                  }
          }
      }
}

class Consumer implements Runnable{
    private Info info = null;
    public Consumer(Info info){
        this.info=info;
    }
    public void run(){
        for(int i=0;i<20;i++){
            try
            {
                Thread.sleep(130);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
          this.info.get();
      }
    }
}

public class ScxfTestDemo1
{
    public static void main(String[] args)
    {
        Info i = new Info();
        producer producer = new producer(i);
        Consumer consumer = new Consumer(i);

        new Thread(producer).start();
        new Thread(consumer).start();
    }

}

这里写图片描述
这里写图片描述

package cn.edu.wic.DXCsisuoTest;


class Info3 {
    private String name = "zjl";
    private String context = "加油,努力";
    boolean flag = false;
    public String getName()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public String getContext()
    {
        return context;
    }
    public void setContext(String context)
    {
        this.context = context;
    }

    public synchronized void set(String name, String context){  //同步方法
        if(!flag){

             try
            {
                super.wait();
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
             this.setName(name);
             try
            {
                Thread.sleep(30);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
             this.setContext(context);
           flag = false;
           super.notify();

        }

    public synchronized void get(){             
        if(flag){
        try
        {
            super.wait();   //由于wait方法是Object类对线程的一个方法,而且是final修饰的方法,所以只能是调用父类的方法,使用super
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
         }
        }
     try
        {
            Thread.sleep(30);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        System.out.println(this.getName()+"---"+this.getContext());
        flag = true;
        super.notify();
     }
    }


class Producer3 implements Runnable{
    private Info3 info = null;
    public Producer3 (Info3 info){  
        this.info=info;
    }
    public void run(){
       boolean flag = false;
       for(int i=0;i<20;i++){
           if (flag)
        {
            this.info.set("zjl", "加油,努力");
            flag = false;
        }
        else
        {
             this.info.set("zjl", "我一定会成功");
             flag=true;
        }
       }
    }
}

class Consumer3 implements Runnable{
    private Info3 info =null;
    public Consumer3(Info3 info){
        this.info = info;
    }
    public void run(){
        for(int i=0;i<20;i++){
            this.info.get();
        }
    }
}

public class ScxfTestDemo3
{

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        Info3 info = new Info3();
        Producer3 producer3 = new Producer3(info);
        Consumer3 consumer3 = new Consumer3(info);

        new Thread(producer3).start();
        new Thread(consumer3).start();
    }

}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值