线程的基础学习之创建线程

来源:互联网 发布:sql字符串中的单引号 编辑:程序博客网 时间:2024/06/04 13:07

线程一般有两种方式实现,一是继承Thread,二是实现Runnable接口。
不用多说,看一个实例代码就清楚了。

public class ThreadTest {    public static void main(String[] args) {        Thread thread = new Thread(){            @Override            public void run() {                while(true){                    try {                        Thread.sleep(500);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                    System.out.println(Thread.currentThread().getName());                    System.out.println(this.getName());                }            }        };        thread.start();        Thread thread2 = new Thread(new Runnable() {            @Override            public void run() {                while(true){                    try {                        Thread.sleep(500);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                    System.out.println(Thread.currentThread().getName());                }            }        });        thread2.start();    }   }

另外,如果既有那个Thread的run()又实现Runnable的run(),会执行哪个run()?
代码:

    new Thread(new Runnable() {            @Override            public void run() {                while(true){                    try {                        Thread.sleep(500);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                    System.out.println("RUNNABLE:"+Thread.currentThread().getName());                }            }        }){            public void run() {                while(true){                    try {                        Thread.sleep(500);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                    System.out.println("THREAD:"+Thread.currentThread().getName());                }            };        }.start();

通过,检测先执行Thread的,如果Thread没有run(),就执行Runnable的run();

如何做到线程互斥,也就是线程同步?首先我们回想到synchronized关键字修饰。可以修饰方法,也可以修饰代码块;synchronized相当于一把锁,修饰代码块,要用同一把锁。
代码示例,一个字符一个字符的打印名字:

public class SynchronizedTest {    public static void main(String[] args) {        new SynchronizedTest().init();    }    class Outputer{        /*public synchronized void output(String name){            for (int i = 0; i < name.length(); i++) {                System.out.print(name.charAt(i));            }            System.out.println();        }*/        /*public  void output(String name){            synchronized (this) {                for (int i = 0; i < name.length(); i++) {                    System.out.print(name.charAt(i));                }                System.out.println();            }        }*/        public  void output(String name){            synchronized (SynchronizedTest.class) {                for (int i = 0; i < name.length(); i++) {                    System.out.print(name.charAt(i));                }                System.out.println();            }        }    }    private  void init() {        Outputer outputer = new Outputer();        new Thread(new Runnable() {            @Override            public void run() {                while(true){                    try {                        Thread.sleep(100);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                    outputer.output("zhangsanfeng");                }            }        }).start();        new Thread(new Runnable() {            @Override            public void run() {                while(true){                    try {                        Thread.sleep(100);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                    outputer.output("zhangwuji");                }            }        }).start();    }}
原创粉丝点击