Java中的线程

1、线程的概念

        程序是计算机指令的集合,它以文件形式存储在磁盘上,而进程就是一个执行中的程序,而每一个进程都有其独立的内存空间和系统资源。进程就是一个运行的程序,Windows操作系统是支持多进程的操作系统。

       线程是运行程序(进程)中单个顺序的小程序,一个进程可以由多个线程组成,而这多个线程共享同一个存储空间,这使得线程间的通信比较容易。多线程指单个程序可通过同时运行多个不同的线程,以执行不同的任务。

2、线程的创建

        在Java语言中,可通过系统提供的编程接口去创建线程。

        创建线程的方法一般有两种:

        ①通过Runnable接口的方式创建线程

在Java中,线程是一种对象,但不是所有的对象都可以称为线程,只有实现了Runnable接口的类,才可以称为线程。

       Runnable接口的定义:

       public interface Runnable

       {

              public abstract void run();

       }

Runnable接口只有一个抽象方法run(),要实现这个接口,只要实现这个抽象方法就可以。只有实现了这个接口的类,才有资格称为线程。创建线程的结构如下:Thread t=new Thread(runnable 对象);

runnable对象是指实现了Runnable接口类的对象。当线程执行时,runnable对象中的run()方法会被调用。如果想要运行上面创建的线程,还需要调用一个Thread类的方法:t.start();

eg:

public class threadstudy

{

    public static void main(String[] args)

    {

        mythread mt=new mythread();

        mythread1 mt1=new mythread1();

        Thread t=new Thread(mt);

        Thread t1=new Thread(mt1);

        t.start();

        t1.start();

    }

}

    class mythread implements Runnable

    {

        int i=0;

        public void run()

        {

            for(int i=0;i<10;i++)

            {

                System.out.println(i);

            }

        }

    }

class mythread1 implements Runnable

    {

        public void run()

        {

            for(int i=0;i<10;i++)

            {

                System.out.println("这个数字是"+i);

            }

        }

    }


        ②通过继承Thread类来创建线程

其实Thread类本身也实现了Runnable接口,所以只要让一个类能够继承Thread类,并覆盖run()方法,也就创建了线程。

public class threadstudy1

{

    public static void main(String[] args)

    {

        mythread t=new mythread();

        mythread1 t1=new mythread1();

        t.start();

        t1.start();

    }

}

    class mythread extends Thread

    {

        int i=0;

        public void run()

        {

            for(int i=0;i<10;i++)

            {

                System.out.println(i);

            }

        }

    }

class mythread1 extends Thread

    {

        public void run()

        {

            for(int i=0;i<10;i++)

            {

                System.out.println("这个数字是"+i);

            }

        }

    }

3、线程的使用

①线程的优先级

        线程的执行时一种抢占方式,优先级高的比优先级低的要获得更多的执行时间,如果想让一个线程比其他线程有更多的时间运行,可以通过设置线程的优先级解决。

      如一个线程创建后,可通过在线程中调用setPriority()方法,来设置其优先级。具体方法如下:

public final void setPriority(int newPriority);

newPriority是一个1~10之间的正整数,数值越大,优先级别越高。系统有一些常数值,如下所示。

public final static int MIN_PRIORITY=1:表示最低优先级。

public final static int MAX_PRIORITY=10:表示最高优先级。

public final static int NORM_PRIORITY=5:表示默认优先级。

public class threadstudy2

{

    public static void main(String[] args)

    {

        mythread t=new mythread();

        mythread1 t1=new mythread1();

        t.setPriority(10);

        t1.setPriority(1);

        t.start();

        t1.start();

        try

       {

           Thread.sleep(5000);

       }

      catch(InterruptedException e){}

    }

}

    class mythread extends Thread

    {

        int i=0;

        public void run()

        {

            for(int i=0;i<10;i++)

            {

                System.out.println(i);

            }

        }

    }

class mythread1 extends Thread

    {

        public void run()

        {

            for(int i=0;i<10;i++)

            {

                System.out.println("这个数字是"+i);

            }

        }

    }

②线程的休眠与唤醒

线程的休眠

线程的休眠,是指线程暂时处于等待的一种状态。通俗地说,就是线程暂时停止运行了。要达到这种功能需要调用Thread类的sleep()方法。sleep()方法可以使线程在指定的时间,处于暂时停止的状态,等到指定时间结束后,暂时停止状态就会结束,然后继续执行没有完成的任务。sleep()方法的方法结构如下所示。

public static native void sleep(long millis) throws interruptedException

millis参数是指线程休眠的毫秒数。

public class threadstudy3

{

    public static void main(String[] args)

    {

        mythread t=new mythread();

        mythread1 t1=new mythread1();

        t.start();

        t1.start();

    }

}

    class mythread extends Thread

    {

        int i=0;

        public void run()

        {

            for(int i=0;i<10;i++)

            {

                System.out.println(i);

                try

                  {

                      sleep(1000);

                   }

                catch(Exception e){}

            }

        }

    }

class mythread1 extends Thread

    {

        public void run()

        {

            for(int i=0;i<10;i++)

            {

                System.out.println("这个数字是"+i);

                 try{sleep(1000);}catch(Exception e){}

            }

        }

    }

线程的唤醒

线程的唤醒是指,使线程从休眠等待状态进入可执行状态,可以调用方法interrupt()实现。

public class threadstudy4

{

    public static void main(String[] args)

    {

        mythread t=new mythread();

        t.start();

        t.interrupt();

    }

}

    class mythread extends Thread

    {

        int i=0;

        public void run()

        {

           

                System.out.println("在工作中,不要打扰!");

                try

                  {

                      sleep(1000000);

                   }

                catch(Exception e){System.out.println("哦,电话来了!");}

            

        }

    }

③线程让步

所谓线程让步,就是使当前正在运行的线程对象退出运行状态,让其他线程运行,其方法是通过调用yield()来实现。这个方法不能将运行权让给指定的线程,只是允许这个线程把运行权让出来,至于给谁,这就是抢占功能的事情了。

public class threadstudy

{

    public static void main(String[] args)

    {

        mythread t=new mythread();

        mythread1 t1=new mythread1();

        t.start();

        t1.start();

    }

}

    class mythread extends Thread

    {

        int i=0;

        public void run()

        {

            for(int i=0;i<10;i++)

            {

                System.out.println(i);

                yield();

            }

        }

    }

class mythread1 extends Thread

    {

        public void run()

        {

            for(int i=0;i<10;i++)

            {

                System.out.println("这个数字是"+i);

            }

        }

    }

④线程同步

线程的运行权通过一种叫抢占的方式获得。一个程序运行到一半时,突然被另一个线程抢占了运行权,此时这个线程数据处理了一半,而另一个线程也在处理这个数据,那么会出现重复操作数据的现象,最终整个系统将会混乱。

public class threadstudy7

{

    public static void main(String[] args)

    {

        mythread t=new mythread('a');

        mythread t1=new mythread('b');

        t.start();

        t1.start();

    }

}

    class mythread extends Thread

    {

        char ch;

        mythread(char ch)

      {

      this.ch=ch;

       }

      public void print(char ch)

      {

            for(int i==0;i<10;i++)

            {

                 System.out.print(ch);

             }

       }

        public void run()

        {

          

                print(ch);

               System.out.println(  );

        }

    }

修改后看线程同步问题,结果存在抢占现象,一会a一会b

public class threadstudy8

{

    public static void main(String[] args)

    {

        mythread t=new mythread('a');

        mythread t1=new mythread('b');

        t.start();

        t1.start();

    }

}

    class mythread extends Thread

    {

        char ch;

        mythread(char ch)

      {

      this.ch=ch;

       }

      public void print(char ch)

      {

            for(int i==0;i<10;i++)

            {

                 System.out.print(ch);

             }

       }

        public void run()

        {

            for(int i=0;i<10;i++)

            {

                print(ch);

               System.out.println(  );

            }

        }

    }

在java语言中,解决同步问题的方法有两种:一种是同步块,一种是同步方法。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值