zoukankan      html  css  js  c++  java
  • Java在不同线程中运行代码

    start()方法开始为一个线程分配CPU时间,这导致对run()方法的调用。

    代码1

    复制代码
    package Threads;
    
    /**
     * Created by Frank
     */
    public class ThreadsDemo1 extends Thread {
        private String msg;
        private int count;
    
        public ThreadsDemo1(final String msg, int n) {
            this.msg = msg;
            count = n;
            setName(msg + " runner Thread");
        }
    
        public void run() {
            while (count-- > 0) {
                System.out.println(msg);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    return;
                }
            }
            System.out.println(msg + " all done.");
        }
    
        public static void main(String[] args) {
            new ThreadsDemo1("Hello from X", 10).start();
            new ThreadsDemo1("Hello from Y", 15).start();
        }
    }
    复制代码

    代码2:

    复制代码
    package Threads;
    
    /**
     * Created by Frank
     */
    public class ThreadsDemo2 implements Runnable {
        private String msg;
        private Thread t;
        private int count;
    
        public static void main(String[] args) {
            new ThreadsDemo2("Hello from X", 10);
            new ThreadsDemo2("Hello from Y", 15);
        }
    
        public ThreadsDemo2(String m, int n) {
            this.msg = m;
            count = n;
            t = new Thread(this);
            t.setName(msg + "runner Thread");
            t.start();
        }
    
        @Override
        public void run() {
            while (count-- > 0) {
                System.out.println(msg);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    return;
                }
            }
            System.out.println(msg + " all done.");
        }
    }
    复制代码

    代码3:

    复制代码
    package Threads;
    
    /**
     * Created by Frank
     */
    public class ThreadsDemo3 {
        private int count;
    
        public static void main(String[] args) {
            new ThreadsDemo3("Hello from X", 10);
            new ThreadsDemo3("Hello from Y", 15);
        }
        public ThreadsDemo3(final String msg, int n) {
            this.count = n;
            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    while (count-- > 0) {
                        System.out.println(msg);
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            return;
                        }
                    }
                    System.out.println(msg + " all done.");
                }
            });
            t.setName(msg + " runner Thread");
            t.start();
        }
    }
    复制代码
  • 相关阅读:
    SQL Server 存储过程/触发器中调用COM组件的方法
    写入Stream
    Python 3.2 中adodbapi的问题
    Python中将系统输出显示在PyQt中
    动态创建 Lambda 表达式
    Entity Framework框架Code First Fluent API
    扩展IQueryable实现属性名称排序
    在Entity Framework中使用事务
    ASP.NET MVC:通过FileResult向浏览器发送文件
    ASP.NET MVC: 使用自定义 ModelBinder 过滤敏感信息
  • 原文地址:https://www.cnblogs.com/chinaifae/p/10238556.html
Copyright © 2011-2022 走看看