zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然java开发常用类库学习笔记:多线程基础编程

    class MyThread implements Runnable{    // 实现Runnable接口,作为线程的实现类
        private String name ;        // 表示线程的名称
        public MyThread(String name){
            this.name = name ;        // 通过构造方法配置name属性
        }
        public void run(){    // 覆写run()方法,作为线程 的操作主体
            for(int i=0;i<10;i++){
                System.out.println(name + "运行,i = " + i) ;
            }
        }
    };
    public class RunnableDemo01{
        public static void main(String args[]){
            MyThread mt1 = new MyThread("线程A ") ;     // 实例化对象
            MyThread mt2 = new MyThread("线程B ") ;     // 实例化对象
            Thread t1 = new Thread(mt1) ;        // 实例化Thread类对象
            Thread t2 = new Thread(mt2) ;        // 实例化Thread类对象
            t1.start() ;    // 启动多线程
            t2.start() ;    // 启动多线程
        }
    };
    class MyThread implements Runnable{    // 继承Thread类,作为线程的实现类
        private int ticket = 5 ;        // 表示一共有5张票
        public void run(){    // 覆写run()方法,作为线程 的操作主体
            for(int i=0;i<100;i++){
                if(this.ticket>0){
                    System.out.println("卖票:ticket = " + ticket--) ;
                }
            }
        }
    };
    public class RunnableDemo02{
        public static void main(String args[]){
            MyThread mt = new MyThread() ;     // 实例化对象
            new Thread(mt).run() ;    // 调用线程主体
            new Thread(mt).run() ;    // 调用线程主体
            new Thread(mt).run() ;    // 调用线程主体
        }
    };
    class MyThread extends Thread{    // 继承Thread类,作为线程的实现类
        private String name ;        // 表示线程的名称
        public MyThread(String name){
            this.name = name ;        // 通过构造方法配置name属性
        }
        public void run(){    // 覆写run()方法,作为线程 的操作主体
            for(int i=0;i<10;i++){
                System.out.println(name + "运行,i = " + i) ;
            }
        }
    };
    public class ThreadDemo01{
        public static void main(String args[]){
            MyThread mt1 = new MyThread("线程A ") ;     // 实例化对象
            MyThread mt2 = new MyThread("线程B ") ;     // 实例化对象
            mt1.run() ;    // 调用线程主体
            mt2.run() ;    // 调用线程主体
        }
    };
    class MyThread extends Thread{    // 继承Thread类,作为线程的实现类
        private String name ;        // 表示线程的名称
        public MyThread(String name){
            this.name = name ;        // 通过构造方法配置name属性
        }
        public void run(){    // 覆写run()方法,作为线程 的操作主体
            for(int i=0;i<10;i++){
                System.out.println(name + "运行,i = " + i) ;
            }
        }
    };
    public class ThreadDemo02{
        public static void main(String args[]){
            MyThread mt1 = new MyThread("线程A ") ;     // 实例化对象
            MyThread mt2 = new MyThread("线程B ") ;     // 实例化对象
            mt1.start() ;    // 调用线程主体
            mt2.start() ;    // 调用线程主体
        }
    };
    class MyThread extends Thread{    // 继承Thread类,作为线程的实现类
        private String name ;        // 表示线程的名称
        public MyThread(String name){
            this.name = name ;        // 通过构造方法配置name属性
        }
        public void run(){    // 覆写run()方法,作为线程 的操作主体
            for(int i=0;i<10;i++){
                System.out.println(name + "运行,i = " + i) ;
            }
        }
    };
    public class ThreadDemo03{
        public static void main(String args[]){
            MyThread mt1 = new MyThread("线程A ") ;     // 实例化对象
            mt1.start() ;    // 调用线程主体
            mt1.start() ;    // 错误
        }
    };
    class MyThread extends Thread{    // 继承Thread类,作为线程的实现类
        private int ticket = 5 ;        // 表示一共有5张票
        public void run(){    // 覆写run()方法,作为线程 的操作主体
            for(int i=0;i<100;i++){
                if(this.ticket>0){
                    System.out.println("卖票:ticket = " + ticket--) ;
                }
            }
        }
    };
    public class ThreadDemo04{
        public static void main(String args[]){
            MyThread mt1 = new MyThread() ;     // 实例化对象
            MyThread mt2 = new MyThread() ;     // 实例化对象
            MyThread mt3 = new MyThread() ;     // 实例化对象
            mt1.run() ;    // 调用线程主体
            mt2.run() ;    // 调用线程主体
            mt3.run() ;    // 调用线程主体
        }
    };
  • 相关阅读:
    Windows Azure Platform Introduction (6) Windows Azure应用程序运行环境
    Windows Azure Platform Introduction (2) 云计算的分类和服务层次
    【转载】修改oracle的最大连接数 以及 object is too large to allocate on this o/s
    Windows Azure Platform Introduction (3) 云计算的特点
    Windows Azure Platform Introduction (8) Windows Azure 账户管理
    XML手册地址
    用dataset方式取值
    xml dataset的发布
    虚惊一场
    XML的一些特点
  • 原文地址:https://www.cnblogs.com/tszr/p/12152826.html
Copyright © 2011-2022 走看看