Java面试题二

开发 后端
本文总结了一些常见的java面试题。

1.拷贝一个目录(文件)到指定路径


   
/** 
    *拷贝一个目录或者文件到指定路径下 BR>    *@paramsource
    *@paramtarget
    */
    publicvoid copy(File source,File target)
    {
      File tarpath = new File(target,source.getName());
      if(source.isDirectory())
      {
          tarpath.mkdir();
          File[] dir = source.listFiles();
          for (int i = 0; i < dir.length; i++) {
              copy(dir[i],tarpath);
          }
      }else
      {
          try {
              InputStream is = new FileInputStream(source);
              OutputStream os = new FileOutputStream(tarpath);
              byte[] buf = newbyte[1024];
              int len = 0;
              while((len = is.read(buf))!=-1)
              {
                  os.write(buf,0,len);
              }
              is.close();
              os.close();
          } catch (FileNotFoundException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
    }

2.用JAVA中的多线程示例银行取款问题

packagecom.softeem.demo; 

/**
*@authorleno
*账户类
*默认有余额,可以取款
*/
class Account {
    privatefloatbalance = 1000;

    publicfloat getBalance() {
      returnbalance;
    }

    publicvoid setBalance(float balance) {
      this.balance = balance;
    }
   
    /**
    *取款的方法需要同步
    *@parammoney
    */
    publicsynchronizedvoid withdrawals(float money)
    {
      if(balance>=money)
      {
          System.out.println("被取走"+money+"元!");
          try {
              Thread.sleep(1000);
          } catch (InterruptedException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          balance-=money;
      }
      else
      {
          System.out.println("对不起,余额不足!");
      }
    }
   
}

/**
*@authorleno
*银行卡
*/
class TestAccount1 extends Thread {

    private Account account;
   
   
    public TestAccount1(Account account) {
      this.account = account;
    }


    @Override
    publicvoid run() {
      account.withdrawals(800);
      System.out.println("余额为:"+account.getBalance()+"元!");
    } 
}
/**
*@authorleno
*存折
*/
class TestAccount2 extends Thread {

    private Account account;
    public TestAccount2(Account account) {
          this.account = account;
      }
    @Override
    publicvoid run() {
      account.withdrawals(700);
      System.out.println("余额为:"+account.getBalance()+"元!");
    } 
}

publicclass Test
{
    publicstaticvoid main(String[] args) {
      Account account = new Account();
      TestAccount1 testAccount1 = new TestAccount1(account);
      testAccount1.start();
      TestAccount2 testAccount2 = new TestAccount2(account);
      testAccount2.start();
    }
}

3.用JAVA中的多线程示例火车站售票问题
package com.softeem.demo; 

/**
*@authorleno
*售票类
*/
class SaleTicket implements Runnable {
    inttickets = 100;

    publicvoid run() {
      while (tickets > 0) {
          sale();
//或者下面这样实现
//        synchronized (this) {
//            if (tickets > 0) {
//                System.out.println(Thread.currentThread().getName() + "卖第"
//                      + (100 - tickets + 1) + "张票");
//                tickets--;
//            }
//        }
      }
    }

    publicsynchronizedvoid sale() {
      if (tickets > 0) {
          System.out.println(Thread.currentThread().getName() + "卖第"
                  + (100 - tickets + 1) + "张票");
          tickets--;
      }
    }

}

publicclass TestSaleTicket {

    publicstaticvoid main(String[] args) {
      SaleTicket st = new SaleTicket();
      new Thread(st, "一号窗口").start();
      new Thread(st, "二号窗口").start();
      new Thread(st, "三号窗口").start();
      new Thread(st, "四号窗口").start();

    }
}


4.用JAVA中的多线程示例生产者和消费者问题
package com.softeem.demo; 

class Producer implements Runnable
{
private SyncStack stack;

    public Producer(SyncStack stack) {
    this.stack = stack;
}

    publicvoid run() {
      for (int i = 0; i < stack.getProducts().length; i++) {
          String product = "产品"+i;
          stack.push(product);
          System.out.println("生产了: "+product);
          try
          {
            Thread.sleep(200);
          }
          catch(InterruptedException e)
          {
            e.printStackTrace();
          }


      }
    }
   
}

class Consumer implements Runnable
{
    private SyncStack stack;

    public Consumer(SyncStack stack) {
    this.stack = stack;
}
    publicvoid run() {
      for(int i=0;i           {
          String product =stack.pop();
          System.out.println("消费了: "+product);
          try
          {
            Thread.sleep(1000);
          }
          catch(InterruptedException e)
          {
            e.printStackTrace();
          }

          }

     
    }
}

class SyncStack
{
    private String[] products = new String[10];
    privateintindex;
    publicsynchronizedvoid push(String product)
    {
      if(index==product.length())
      {
          try {
              wait();
          } catch (InterruptedException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
      notify();
      products[index]=product;
      index++;
    }
   
    publicsynchronized String pop()
    {
      if(index==0)
      {
          try {
              wait();
          } catch (InterruptedException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
      notify();
      index--;
      String product = products[index];
      return product;
    }

    public String[] getProducts() {
      returnproducts;
    }
   
   
}
publicclass TestProducerConsumer {
   
    publicstaticvoid main(String[] args) {
      SyncStack stack=new SyncStack();
      Producer p=new Producer(stack);
      Consumer c=new Consumer(stack);

      new Thread(p).start();
      new Thread(c).start();
      }
    }

【编辑推荐】

  1. IT职场培训 程序员Java面试中的32个陷阱
  2. 快速启动一个Java Web编程框架
  3. Java中静态数组与动态数组
  4. 讨论:Java究竟是传值还是传引用?
  5. Java编译器中对String对象的优化
责任编辑:王观 来源: 站长学院
相关推荐

2009-06-06 18:34:05

java面试题

2015-09-02 09:32:56

java线程面试

2020-06-04 14:40:40

面试题Vue前端

2013-05-29 10:23:36

Android开发移动开发Java面试题

2023-11-13 07:37:36

JS面试题线程

2018-03-08 18:40:47

Java百度面试题

2011-03-24 13:27:37

SQL

2021-01-15 07:49:01

嵌入式笔试面试

2018-07-20 09:24:27

Java面试垃圾收集

2017-08-29 14:12:16

Java面试题

2014-09-19 11:17:48

面试题

2015-09-10 08:46:15

Java面试题

2020-08-06 10:45:30

JavaSpring面试题

2023-07-14 08:12:21

计时器unsafecontext

2014-07-28 14:00:40

linux面试题

2013-01-05 14:51:34

JavaScriptjQuery面试

2012-05-25 10:15:06

Java程序员面试题

2009-06-16 14:03:16

Hibernate面试Hibernate面试

2021-02-23 12:43:39

Redis面试题缓存

2020-11-05 10:01:35

系统设计软件
点赞
收藏

51CTO技术栈公众号