zoukankan      html  css  js  c++  java
  • java的线程

    多任务与多线程:多任务属于系统级的各个应用之间的关系,而多线程属于应用级的一个应用的多个功能之间的关系

    创建线程的两种方式:

    实现Runnable接口

    class sunclass {}

    class SumThread extends sumclass imlements Runnable{}

    public class **{public void main(){SumThread st=new SumThread();Thread t=new Thread(st,"threadname");t.start();}} 

    继承Thread类(直接继承thread类,或者使用匿名嵌套类)

    class SumThread extends Thread{
        private long length;
        public SumThread(long length,String name){
            super(name);
            this.length=length;
        }
        public void run(){
            long temp=0;
            for(int i=1;i<=length;i++){
                try {
                    Thread.sleep((int)(Math.random()*10));
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                temp+=i;
            }
            System.out.println(Thread.currentThread()+"zonghe:"+temp);
        }
    }
    public class Thread1 {
        public static void main(String[] args) {
            System.out.println("线程");
            System.out.println(Thread.currentThread());
            SumThread st1=new SumThread(150,"线程1");
            st1.start();
            new Thread("线程2"){
                int length=150;
                public void run(){
                    long temp=0;
                    for(int i=1;i<=length;i++){
                        try {
                            Thread.sleep((int)(Math.random()*10));
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        temp+=i;
                    }
                    System.out.println(Thread.currentThread()+"zonghe:"+temp);
                }
            }.start();
    }}
  • 相关阅读:
    __cdecl, __stdcall, __fastcall,__pascal调用区别
    Windows Hook原理与实现
    C语言四大存储区域总结
    MFC DestroyWindow、OnDestroy、OnClose 程序关闭相关
    VC++动态链接库DLL编程深入浅出"
    windows 安全模型简介
    获取当前焦点窗口进程名
    获取IE URL
    DLL编写中extern “C”和__stdcall的作用
    Django2支持跨域方法
  • 原文地址:https://www.cnblogs.com/zzy-frisrtblog/p/5463389.html
Copyright © 2011-2022 走看看