• Java 复习 之1 多线程


    0  转自  http://www.cnblogs.com/rollenholt/archive/2011/08/28/2156357.html  

    (1)Java中每个程序至少启动2个线程,一个main线程,一个垃圾回收线程,

    (2)判断线程是否启动的方法: isAlive()

    (3) 线程的强制执行: join()

     (4) 线程的休眠: sleep()  Thread.sleep(2000);

    (5) demo.interrupt(); //2s后中断线程

    (6)demo.setDaemon(true); //后台运行线程

    (7) h1.setPriority(8);//线程的优先级,优先级的高低与谁 先执行 还不是必然联系的

    (8)线程的 礼让:yield 

    (9) 线程的同步和死锁,所谓同步,是指在同一个时间段内只有一个线程在运行,其他线程必须等到这个线程执行完毕后才执行

    Java中的同步有两种方式: 同步代码块和同步方法

    synchronized  一般将当前对象this作为同步对象

    【同步代码块】:

    语法格式:

    synchronized(同步对象){

     //需要同步的代码

    }

    【同步方法】

    也可以采用同步方法。

    语法格式为synchronized 方法返回类型 方法名(参数列表){

        // 其他代码

    }


    1. 在Java中实现多线程有两种方式,实现Runnable 接口,或者 extends 自Thread类,其实Thread类也是实现了Runnable接口,复写run()方法

    // Java 中的 多线程 有两种方式,一个继承自Thread,一种是实现Runnable 接口
    class Hello extends Thread
    {
    private String name;
    public Hello()
    {

    }
    public Hello(String name)
    {
    this.name = name;
    }
    public void run()
    {
    for(int i = 0;i < 10;i ++)
    {
    System.out.println(name
    + "运行 " + i);
    }
    }

    public static void main(String [] args)
    {
    Hello h1
    = new Hello("hello1");
    Hello h2
    = new Hello("hello2");
    h1.start();
    h2.start();
    }

    }

      

    public class hello2 implements Runnable{
    private String name;
    public hello2()
    {

    }
    public hello2(String name)
    {
    this.name = name;
    }

    @Override
    public void run() {
    // TODO Auto-generated method stub
    for(int i = 0;i < 10;i ++)
    {
    System.out.println(name
    + "运行" + i);
    }
    }
    public static void main(String [] args)
    {
    hello2 h1
    = new hello2("hello1");
    hello2 h2
    = new hello2("hello2");
    Thread t1
    = new Thread(h1);
    Thread t2
    = new Thread(h2);

    t1.start();
    t2.start();

    }
    }

      不过两种方式的不同之处在于,实现Runnable接口的,还要使用Thread 去包装一下下 ,就是所谓的代理模式

        区别,到底选择哪种呢?

       如果不需要共享资源,继承Thread就行,Runnable接口的则是需要共享资源

    /**
    *
    @author Rollen-Holt 继承Thread类,不能资源共享
    *
    */
    class hello extends Thread {
    public void run() {
    for (int i = 0; i < 7; i++) {
    if (count > 0) {
    System.out.println(
    "count= " + count--);
    }
    }
    }

    public static void main(String[] args) {
    hello h1
    = new hello();
    hello h2
    = new hello();
    hello h3
    = new hello();
    h1.start();
    h2.start();
    h3.start();
    }

    private int count = 5;
    }

      而选择实现接口,而可以实现资源的共享

    /**
    *
    @author Rollen-Holt 继承Thread类,不能资源共享
    *
    */
    class hello implements Runnable {
    public void run() {
    for (int i = 0; i < 7; i++) {
    if (count > 0) {
    System.out.println(
    "count= " + count--);
    }
    }
    }

    public static void main(String[] args) {
    hello he
    =new hello();
    new Thread(he).start();
    }

    private int count = 5;
    }

      实现Runnable接口比继承Thread类所具有的优势:

    1):适合多个相同的程序代码的线程去处理同一个资源

    2):可以避免java中的单继承的限制

    3):增加程序的健壮性,代码可以被多个线程共享,代码和数据独立。



    Thread.currentThread().getName()用于获得线程的名字

    如果我们没有指定名字的话,系统自动提供名字

    thread-0 main方法其实也是一个线程

    Java中每个程序都至少启动两个线程,一个main线程,一个垃圾回收线程




    经典的生产者消费者

    解决方案:使用同步,加入等待和唤醒

  • 相关阅读:
    java多线程--定时器Timer的使用
    java多线程--线程池的使用
    java多线程--多线程基础小结
    java基础--java.util.Date类型小结
    onClickRow 事件
    C#里的SubString和Convert.ToDateTime
    jquery+easyui开发、培训文档
    Jquery函数大全
    前端最全的 API 集锦
    17个CSS知识点整理
  • 原文地址:https://www.cnblogs.com/hitwtx/p/2156973.html
Copyright © 2020-2023  润新知