日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Java面試題二

1.拷貝一個(gè)目錄(文件)到指定路徑

   

/** 
    *拷貝一個(gè)目錄或者文件到指定路徑下 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
*賬戶類
*默認(rèn)有余額,可以取款
*/
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("對(duì)不起,余額不足!");
      }
    }
   
}

/**
*@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();
//或者下面這樣實(shí)現(xiàn)
//        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, "一號(hào)窗口").start();
      new Thread(st, "二號(hào)窗口").start();
      new Thread(st, "三號(hào)窗口").start();
      new Thread(st, "四號(hào)窗口").start();

    }
}

4.用JAVA中的多線程示例生產(chǎn)者和消費(fèi)者問題

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 = "產(chǎn)品"+i;
          stack.push(product);
          System.out.println("生產(chǎn)了: "+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("消費(fèi)了: "+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();
      }
    }

新聞名稱:Java面試題二
本文來源:http://www.dlmjj.cn/article/djchsip.html