码迷,mamicode.com
首页 > 编程语言 > 详细

java线程跟多线程

时间:2016-04-24 21:52:02      阅读:407      评论:0      收藏:0      [点我收藏+]

标签:

java创建线程两种方式:

1.继承Thread创建线程

/**
 * Created by lsf on 16/4/18.
 */
class NewThread extends Thread {
    NewThread(){
        super();    //创建线程
        start();    //启动线程
    }

    public void run() {
        long starttime = System.currentTimeMillis();
        System.out.println("child thread..."+starttime);
    }
}

class CurrentThreadDemo {
    public static void main(String args[]) {
        long starttime2 = System.currentTimeMillis();
        System.out.println("main thread,,,"+starttime2);    //主线程
        new NewThread();
        System.out.println("住县城");
    }
}

2.实现

import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIConversion;

import java.util.Date;

/**
 * Created by lsf on 16/4/18.
 */
class NewThread extends Thread {
    NewThread(){
        Thread t = new Thread(this);    //创建线程
        t.start();                      //启动线程
    }

    public void run() {
        long starttime = System.currentTimeMillis();
        System.out.println("child thread..."+starttime);
    }
}

class CurrentThreadDemo {
    public static void main(String args[]) {
        long starttime2 = System.currentTimeMillis();
        System.out.println("main thread,,,"+starttime2);
        new NewThread();
        System.out.println("主线程");  //主线程
    }
}

 3.给任务创建多个线程去执行

class NewThread extends Thread {
    String name;
    NewThread(String threadname){
        name = threadname;
        Thread t = new Thread(this,threadname);    //创建线程
        t.start();                      //启动线程
    }

    public void run() {
        long starttime = System.currentTimeMillis();
        System.out.println("child thread..."+name);
    }
}

class CurrentThreadDemo {
    public static void main(String args[]) {
        long starttime2 = System.currentTimeMillis();
        System.out.println("main thread,,,"+starttime2);
        new NewThread("demo1");
        new NewThread("demo2");
        new NewThread("demo3");
        System.out.println("主线程");  //主线程
    }
}

 4.线程优先级设置

/**
 * Created by lsf on 16/4/22.
 */

class ThreadTest implements Runnable {

    Thread t;
    int count = 0;
    private volatile Boolean flag = true;

    public ThreadTest(int p) {
        t = new Thread(this);
        t.setPriority(p);
    }

    public void start(){
        t.start();
    }

    public void finish(){
        flag = false;
    }

    @Override
    public void run() {
        while(flag){
            count++;
        }
    }

}

public class ThreadPriority {
    public static void main(String[] args) {
        ThreadTest t1 = new ThreadTest(Thread.NORM_PRIORITY - 2);
        ThreadTest t2 = new ThreadTest(Thread.NORM_PRIORITY + 2);
        t1.start();
        t2.start();
        t1.finish();
        t2.finish();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        try {
            System.out.println("t1 count:"+t1.count);
            System.out.println("t2 count:"+t2.count);
            t1.t.join();
            t2.t.join();
            System.out.println("t1 is alive:" + t1.t.isAlive());
            System.out.println("t2 is alive:" + t1.t.isAlive());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

 5.线程同步

线程同步的关键在于同一时刻线程在管程内,应用场景一般是:当某个方法(callme)需要用多线程去执行,可以改造一下对应的方法,加上关键词synchronized,这样在调用过程中,每个线程都会默认进入隐式管程。

/**
 * Created by root on 16-4-15.
 */

class Callme {
    synchronized void call(String msg){
        System.out.println("["+msg);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("]");
    }
}

class ThreadCaller implements Runnable{

    String msg;
    Callme target;
    Thread t;

    public ThreadCaller(Callme targ,String s) {
        target = targ;
        msg = s;
        t = new Thread(this);
        t.start();
    }

    @Override
    public void run() {
        target.call(msg);
    }
}

class Demo {
    public static void main(String[] args) {
        Callme target = new Callme();
        ThreadCaller obj1 = new ThreadCaller(target,"Hello");
        ThreadCaller obj2 = new ThreadCaller(target,"Synchronized");
        ThreadCaller obj3 = new ThreadCaller(target,"World");

        try {
            obj1.t.join();
            obj2.t.join();
            obj3.t.join();
        } catch (InterruptedException e) {
            System.out.println("Interrupted");
        }
    }
}

  

java线程跟多线程

标签:

原文地址:http://www.cnblogs.com/alexkn/p/5418084.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!