Java常见题型

要求:使用定时器,间隔 4 秒执行一次,再间隔 2 秒执行一次,以此类推执行

package multiThread;
 
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
 
public class TimerTest extends TimerTask {
    private static volatile int count = 0;
    @Override
    public void run() {
        count = (count+1)%2; //结果为0或1交替
        System.err.println("执行定时任务,隔两秒四秒交替打印");
        new Timer().schedule(new TimerTest(),2000+2000*count);
    }
 
    public static void main(String[] args) {
        Timer timer = new Timer(); //设置一个定时器
        timer.schedule(new TimerTest(),2000); //2秒后执行TimerTest中的run方法
        while(true){ //每隔一秒打印一次
            System.out.println(Calendar.getInstance().get(Calendar.SECOND));
            try{
                Thread.sleep(1000);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    }
}

用面向对象的方法求出数组中重复 value 的个数,按如下个数输出:
1 出现:1 次
3 出现:2 次
8 出现:3 次
2 出现:4 次
int[] arr = {1,4,1,4,2,5,4,5,8,7,8,77,88,5,4,9,6,2,4,1,5};

package com.xzq;
 
import java.util.HashMap;
import java.util.Map;
 
public class ArrayDemo {
    public static void main(String[] args) {
        int[] arr = {1, 4, 1, 4, 2, 5, 4, 5, 8, 7, 8, 77, 88, 5, 4, 9, 6, 2, 4, 1, 5};
 
        ArrayDemo arrayDemo = new ArrayDemo();
        arrayDemo.countAndSout(arr);
    }
 
    public void countAndSout(int arr[]) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < arr.length; i++) {
            int a = arr[i];
            if (!map.containsKey(a)) {
                map.put(a, 1);
            } else {
                Integer integer = map.get(a);
                integer++;
                map.put(a, integer);
            }
        }
 
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            Integer key = entry.getKey();
            Integer value = entry.getValue();
            System.out.println(key + " 出现:" + value + "次");
        }
    }
}
import java.util.TreeMap;

public class Main {
	public static void main(String[] args) {
		int[] arr = { 1, 8, 2, 2, 8, 9, 8, 0, 6, 8, 7, 4, 5, 8, 3, 2, 2, 3 };
		calculate(arr);
	}

	public static void calculate(int[] arr) {
		TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
		for (int i = 0; i < arr.length; i++) {
			if (map.containsKey(arr[i])) {
				map.put(arr[i], map.get(arr[i]) + 1);
			} else {
				map.put(arr[i], 1);
			}
		}
		System.out.println("统计后,输出前:");
		System.out.println(map);
		int n = map.size();
		for (int i = 0; i < n; i++) {
			System.out.println(map.firstKey() + "出现:" + map.get(map.firstKey()) + "次");
			map.remove(map.firstKey());
		}
		System.out.println("输出后(由于map不能根据index下标索引来获取元素,只能一个一个获取第
		一个键值对,然后删除来实现,所以最终为空的):");
		System.out.println(map);
	}
}

要求:子线程运行执行 10 次后,主线程再运行 5 次。这样交替执行三遍

public static void main(String[] args) {
		final Bussiness bussiness = new Bussiness();
		//子线程
		new Thread(new Runnable() {
			@Override
			public void run() {
				for (int i = 0; i < 3; i++) {
					bussiness.subMethod();
				}
			}
		}).start();
		//主线程
		for (int i = 0; i < 3; i++) {
			bussiness.mainMethod();
		}
	}
}

class Bussiness {
	private boolean subFlag = true;
	public synchronized void mainMethod() {
		while (subFlag) {
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
		for (int i = 0; i < 5; i++) {
			System.out.println(Thread.currentThread().getName()+ " : main thread running loop count -- " + i);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		subFlag = true;
		notify();
	}
	
	public synchronized void subMethod() {
		while (!subFlag) {
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		for (int i = 0; i < 10; i++) {
			System.err.println(Thread.currentThread().getName() + " : sub thread running loop count -- " + i); 
			try {
					Thread.sleep(1000); 
				} catch (InterruptedException e) {
					e.printStackTrace();
			}
		}
		subFlag = false;
		notify();
	}
}
/**
 * 子线程运行执行10次后,主线程再运行5次。这样交替执行三遍
 */
public class _02_Interview {

    public static void main(String[] args) {

        Business bussiness = new Business();

        //子线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 3; i++) {
                    bussiness.subMethod();
                }
            }
        }).start();

        //主线程
        for (int i = 0; i < 3; i++) {
            bussiness.mainMethod();
        }
    }

}

//这个类是执行任务的类
class Business {

    private boolean flag = false;

    //flag为true,主线程执行
    public synchronized void mainMethod() {
        //是不是子线程在执行?是,继续等子线程执行完
        while (false == flag) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //执行主线程任务
        for (int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName() + i);
        }
        //轮到子线程执行了
        flag = false;
        //唤醒子线程。(一共就两个线程,子线程和主线程,所以唤醒的只能是子线程。)
        notify();
    }

    //flag为false,子线程执行
    public synchronized void subMethod() {
        //是不是主线程在执行?是,继续等主线程执行完
        while (true == flag) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        //执行子线程任务
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName() + i);
        }
        //轮到主线程执行了
        flag = true;
        //唤醒主线程。(一共就两个线程,子线程和主线程,所以唤醒的只能是主线程。)
        notify();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一、判断题(每题1分,共15分) 1、Java允许创建不规则数组,即Java多维数组中各行的列数可以不同。 ( ) 2、接口和类一样也可以有继承关系,而且都只能支持单继承。 ( ) 3、所有类至少有一个构造器,构造器用来初始化类的新对象,构造器与类同名,返回类型只能为void。 ( ) 4、包是按照目录、子目录存放的,可以在程序中用package定义包,若没有package一行,则表示该文件中的类不属于任何一个包。 ( ) 5、Java对事件的处理是采用委托方式进行的,即将需要进行事件处理的组件委托给指定的事件处理器进行处理。 ( ) 6、在异常处理中,若try中的代码可能产生多种异常则可以对应多个catch语句,若catch中的参数类型有父类子类关系,此时应该将父类放在前面,子类放在后面。 ( ) ..... 二、单项选择题(每题2分,共30分) 1、若在某一个类定义中定义有如下的方法: final void aFinalFunction( );则该方法属于( )。 A、本地方法 B、解态方法 C、最终方法 D、抽象方法 2、main方法是Java Application程序执行的入口点,关于main方法的方法头以下哪项是合法的( )。 A、 public static void main() B、 public static void main(String[ ] args) C、 public static int main(String[ ] args) D、 public void main(String arg[ ]) 3、在Java中,一个类可同时定义许多同名的..... ...... 14、一个线程的run方法包含以下语句,假定线程没有被打断,以下哪项是正确的( ) 1.try{ 2. sleep(100); 3. }catch(InterruptedException e){ } A、不能通过编译,因为在run方法中可能不会捕捉到异常。 B、在第2行,线程将暂停运行,正好在100毫秒后继续运行。 C、在第2行,线程将暂停运行,最多在100毫秒内将继续运行。 D、在第2行,线程将暂停运行,将在100毫秒后的某一时刻继续运行。 15、以下哪个接口的定义是正确的?( ) A、 interface A { void print() { } ;} B、 abstract interface A { void print() ;} C、 abstract interface A extends I1, I2 //I1、I2为已定义的接口 { abstract void print(){ };} D、 interface A { void print();} 三、程序阅读题(1~8题每题4分,第9题占8分,共40分) 1、若文件test.dat不存在,则试图编译并运行以下程序时会发生什么情况? import java.io.*; class TestIO { public static void main(String[] args) { try{ RandomAccessFile raf=new RandomAccessFile("test.dat","r"); int i=raf.readInt(); } catch(IOException e){System.out.println("IO Exception"); } } } 2、以下程序的输出结果为 。 public class EqualsMethod { public static void main(String[] args) { Integer n1 = new Integer(12); Integer n2 = new Integer(12); System.out.print(n1= =n2); System.out.print(“,”); System.out.println(n1! =n2); } } ........ 1、在java中如果声明一个类为final,表示什么意思? 答:final是最终的意思,final可用于定义变量、方法和类但含义不同,声明为final的类不能被继承。 2、父类的构造方法是否可以被子类覆盖(重写)? 答:父类的构造方法不可以被子类覆盖,因为父类和子类的类名是不可能一样的。 3、请讲述String 和StringBuffer的区别。 答:String 类所定义的对象是用于存放“长度固定”的字符串。 StringBuffer类所定义的对象是用于存放“长度可变动”的字符串。 4、如果有两个类A、B(注意不是接口),你想同时使用这两个类的功能,那么你会如何编写这个C类呢? ........

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值