复习java3

8 篇文章 0 订阅
package com.company;


import java.util.Stack;

/**
 * Created by henson on 17-8-29.
 */
public class Stacks {
    static String[] months={"Jan","Febr","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
    public static void main(String[] args){
        Stack stk=new Stack();
        for (int i=0;i<months.length;i++)
            stk.push(months[i]+" ");
        System.out.println("stk="+stk);
        System.out.println("popping elements:");
        while (!stk.empty()){
            System.out.println(stk.pop());
        }
    }
}
package com.company;

/**
 * Created by henson on 17-8-29.
 */
public class SelectSort {
    public static void main(String[] args){
        int i,j;
        int a[]={30,1,-9,70,25};
        int n=a.length;
        for (i=0;i<n-1;i++)
            for (j=i+1;j<n;j++)
                if (a[i]>a[j]){
                    int t=a[i];
                    a[i]=a[j];
                    a[j]=t;
                }
        for (i=0;i<n;i++){
                    System.out.println(a[i]+" ");
        }
    }
}
package com.company;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Map;
import java.util.*;

/**
 * Created by henson on 17-8-29.
 */
public class TestHashtable {
    public static void main(String[] args){
        Map ht=new Hashtable();
        ht.put("one",new Integer(1));
        ht.put("two",new Integer(2));
        ht.put("three",new Integer(3));
        ht.put("four",new Integer(4));
        ht.put("five",new Integer(5));
        Enumeration em = ((Hashtable) ht).keys();
        while (em.hasMoreElements()){
            Object key =em.nextElement();
            Object value=ht.get(key);
            System.out.println(""+key+"="+value);
        }
    }
}
package com.company;

/**
 * Created by henson on 17-8-29.
 */
public class BubbleSort {
    public static  void main(String[] args){
        int i,j;
        int a[]={30,1-10,70,25};
        int n=a.length;
        for (i=1;i<n;i++)
            for(j=0;j<n-1;j++)
                if (a[j]>a[j+1]){
            int t=a[j];
            a[j]=a[j+1];
            a[j+1]=t;
                }
        for (i=0;i<n;i++)
            System.out.println(a[i]+" ");
    }
}
package com.company;

/**
 * Created by henson on 17-8-29.
 */
public class TestThread1 {
    public static void main(String[] args){
        Thread t=new Thread();
        t.start();
    }
}
class MyThread extends Thread{
    public void run(){
        for (int i=0;i<100;i++){
            System.out.print(" "+i);
        }
    }
}
package com.company;

/**
 * Created by henson on 17-8-29.
 */
public class TestThread1 {
    public static void main(String[] args){
        Thread t=new Thread();
        t.start();
    }
}
class MyThread extends Thread{
    public void run(){
        for (int i=0;i<100;i++){
            System.out.print(" "+i);
        }
    }
}
package com.company;

/**
 * Created by henson on 17-8-29.
 */


public class TestThread2 {
     static class Mytask implements Runnable{
        private  int n;
        public Mytask(int n){
            this.n=n;
        }
        public void run(){
            long startTime=System.currentTimeMillis();   //获取开始时间
            for (int i=0;i<n;i++)
                System.out.println(" "+i);
                try {
                    Thread.sleep(500);
                }catch (InterruptedException e){}
            long endTime=System.currentTimeMillis(); //获取结束时间
            System.out.println("程序运行时间: "+(endTime-startTime)+"ms");
        }


    }
    public static void main(String[] args){

        Mytask mytask=new Mytask(1000000);
        Thread t1=new Thread(mytask);
        Thread t2=new Thread(mytask);
        t1.start();
        t2.start();



    }
}
package com.company;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by henson on 17-8-29.
 */
public class TestThrad3 {
    public static void main(String[] args){
        Runner1 r1=new Runner1(1);
        Runner1 r2=new Runner1(2);
        Thread t1=new Thread(r1);
        Thread t2=new Thread(r1);
        Thread t3=new Thread(r1);
        Thread t4=new Thread(r2);
        Thread t5=new Thread(r2);
        Timer timer=new Timer();
        Thread t6=new Thread(timer);
        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();
        t6.start();

    }
}

class Runner1 implements Runnable{
    int id;
    Runner1(int id){
        this.id=id;
    }
    public void run(){
        int i=0;
        while (true){
            i++;
            System.out.println("ID"+id+"NO."+i);
            try {
                Thread.sleep(300);
            }catch (InterruptedException e){}
        }
    }
}

class Timer implements Runnable{
    public void run(){
        while (true){
            System.out.println(new SimpleDateFormat().format(new Date()));
            try {
                Thread.sleep(1000);
            }catch (InterruptedException e){}
        }

    }
}
package com.company;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by henson on 17-8-29.
 */

public class TimerTest {
    public static void main(String[] args){
        Timer timer=new Timer("显示n");
        TimerTask tt=new Mytask3();
        timer.schedule(tt,1000,1000);
        }
    }

class Mytask3 extends TimerTask{
    int n=0;
    public void run(){
        n++;
        System.out.println(new Date());
        System.out.println("---"+n);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值