首页 > Java > java教程 > Java之线程(匿名内部类)

Java之线程(匿名内部类)

高洛峰
发布: 2016-12-15 13:07:39
原创
1345 人浏览过

1 Thread对象 

Java代码 

Thread t = new Thread(  
                new Thread(){  
                    @Override  
                    public void run() {  
                      
                        while(true){  
                            try {  
                                Thread.sleep(1000);  
                            } catch (InterruptedException e) {  
                                e.printStackTrace();  
                            }  
                            System.out.println("1: "+Thread.currentThread().getName());  
                            System.out.println("2: "+this.getName());  
                        }                         
                          
                    }  
                }  
        );  
        t.start();
登录后复制

2 Runnable接口

Java代码

Thread t2 = new Thread(  
                new Runnable(){  
                    @Override  
                    public void run() {  
                          
                        while(true){  
                            try {  
                                Thread.sleep(1000);  
                            } catch (InterruptedException e) {  
                                e.printStackTrace();  
                            }  
                            System.out.println("3: "+Thread.currentThread().getName());                           
                        }                         
                    }  
                }  
        );  
        t2.start();
登录后复制

下面会执行哪部分代码呢!

Java代码

//只要重写了run方法,你从构造函数传递进去的线程对象就不会在执行        
        new Thread(new Runnable(){  
              
            @Override  
            public void run() {  
                  
                while(true){  
                    try {  
                        Thread.sleep(1000);  
                    } catch (InterruptedException e) {  
                        e.printStackTrace();  
                    }  
                    System.out.println("runnable: "+Thread.currentThread().getName());                            
                }                         
            }  
        }  
        ){  
              
            @Override  
            public void run() {  
                  
                while(true){  
                    try {  
                        Thread.sleep(1000);  
                    } catch (InterruptedException e) {  
                        e.printStackTrace();  
                    }  
                    System.out.println("thread: "+Thread.currentThread().getName());                              
                }  
                  
            }  
        }.start();
登录后复制

输出结果如下:

Log代码

3: Thread-2  
1: Thread-1  
2: Thread-0  
thread: Thread-3
登录后复制

以下代码来自于:JAVA编程思想第四版,说明加了是否加线程的区别!
Counter1 .java没有加线程的表现!

Java代码

import java.awt.*;  
import java.awt.event.*;  
import java.applet.*;  
  
public class Counter1 extends Applet {    
      
    private static final long serialVersionUID = 1L;  
    private int count = 0;  
    private Button onOff = new Button("Toggle"), start = new Button("Start");  
    private TextField t = new TextField(10);  
    private boolean runFlag = true;  
  
    public void init() {  
        add(t);  
        start.addActionListener(new StartL());  
        add(start);  
        onOff.addActionListener(new OnOffL());  
        add(onOff);  
    }  
  
    @SuppressWarnings("static-access")  
    public void go() {  
        while (true) {  
            try {  
                Thread.currentThread().sleep(100);  
            } catch (InterruptedException e) {  
            }  
            if (runFlag)  
                t.setText(Integer.toString(count++));  
  
        }  
    }  
  
    class StartL implements ActionListener {  
        public void actionPerformed(ActionEvent e) {  
            go();  
        }  
    }  
  
    class OnOffL implements ActionListener {  
        public void actionPerformed(ActionEvent e) {  
            runFlag = !runFlag;  
        }  
    }  
  
    public static void main(String[] args) {  
        Counter1 applet = new Counter1();  
        Frame aFrame = new Frame("Counter1");  
        aFrame.addWindowListener(new WindowAdapter() {  
            public void windowClosing(WindowEvent e) {  
                System.exit(0);  
            }  
        });  
        aFrame.add(applet, BorderLayout.CENTER);  
        aFrame.setSize(300, 200);  
        applet.init();  
        applet.start();  
        aFrame.setVisible(true);  
    }  
}
登录后复制

Counter2i.java加了线程结果大不一样了,加了个内部类,它继承了Thread

Java代码

import java.awt.*;  
import java.awt.event.*;  
import java.applet.*;  
  
public class Counter2i extends Applet {  
      
    private static final long serialVersionUID = 1L;  
  
    private class SeparateSubTask extends Thread {  
        int count = 0;  
        boolean runFlag = true;  
  
        SeparateSubTask() {  
            start();  
        }  
  
        public void run() {  
            while (true) {  
                try {  
                    sleep(100);  
                } catch (InterruptedException e) {  
                }  
                if (runFlag)  
                    t.setText(Integer.toString(count++));  
            }  
        }  
    }  
  
    private SeparateSubTask sp = null;  
    private TextField t = new TextField(10);  
    private Button onOff = new Button("Toggle"), start = new Button("Start");  
  
    public void init() {  
  
        add(t);  
        start.addActionListener(new StartL());  
        add(start);  
        onOff.addActionListener(new OnOffL());  
        add(onOff);  
    }  
  
    class StartL implements ActionListener {  
        public void actionPerformed(ActionEvent e) {  
            if (sp == null)  
                sp = new SeparateSubTask();  
        }  
    }  
  
    class OnOffL implements ActionListener {  
        public void actionPerformed(ActionEvent e) {  
            if (sp != null)  
                sp.runFlag = !sp.runFlag; // invertFlag();  
        }  
    }  
  
    public static void main(String[] args) {  
        Counter2i applet = new Counter2i();  
        Frame aFrame = new Frame("Counter2i");  
        aFrame.addWindowListener(new WindowAdapter() {  
            public void windowClosing(WindowEvent e) {  
                System.exit(0);  
            }  
        });  
        aFrame.add(applet, BorderLayout.CENTER);  
        aFrame.setSize(300, 200);  
        applet.init();  
        applet.start();  
        aFrame.setVisible(true);  
    }  
}
登录后复制

Counter3.java它实现了Runnable接口

Java代码

import java.awt.*;  
import java.awt.event.*;  
import java.applet.*;  
  
public class Counter3 extends Applet implements Runnable {  
      
    private static final long serialVersionUID = 1L;  
    private int count = 0;  
    private boolean runFlag = true;  
    private Thread selfThread = null;  
    private Button onOff = new Button("Toggle"), start = new Button("Start");  
    private TextField t = new TextField(10);  
  
    public void init() {  
        add(t);  
        start.addActionListener(new StartL());  
        add(start);  
        onOff.addActionListener(new OnOffL());  
        add(onOff);  
    }  
  
    @SuppressWarnings("static-access")  
    public void run() {  
        while (true) {  
            try {  
                selfThread.sleep(100);  
            } catch (InterruptedException e) {  
            }  
            if (runFlag)  
                t.setText(Integer.toString(count++));  
        }  
    }  
  
    class StartL implements ActionListener {  
        public void actionPerformed(ActionEvent e) {  
            if (selfThread == null) {  
                selfThread = new Thread(Counter3.this);  
                selfThread.start();  
            }  
        }  
    }  
  
    class OnOffL implements ActionListener {  
        public void actionPerformed(ActionEvent e) {  
            runFlag = !runFlag;  
        }  
    }  
  
    public static void main(String[] args) {  
        Counter3 applet = new Counter3();  
        Frame aFrame = new Frame("Counter3");  
        aFrame.addWindowListener(new WindowAdapter() {  
            public void windowClosing(WindowEvent e) {  
                System.exit(0);  
            }  
        });  
        aFrame.add(applet, BorderLayout.CENTER);  
        aFrame.setSize(300, 200);  
        applet.init();  
        applet.start();  
        aFrame.setVisible(true);  
    }  
}
登录后复制



更多Java之线程(匿名内部类)相关文章请关注PHP中文网!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板