java

来源:互联网 发布:离坚白 知乎 编辑:程序博客网 时间:2024/06/05 06:51
package test;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class B {  
    public static void main(String[] args) {  
//     way_1();
    way_3();
//     System.out.println(subStringByBytes("我ABC汉DEF", 6,"gbk")); 
    }  
    
  public void way(){
      int[] a = new int[] { 10, 9,12, -70000, -90000, -600000,  
              -500000, -32769 }; 
      
      Arrays.sort(a);
      
      for(int i=0;i<a.length-1;i++)   
       
      {   
  
        System.out.print(a[i]+" ");   
  
      } 
      System.out.println();
      int j = a.length-3;
      
      System.out.println(a[j]+"--");
  }
    
    public static void way_1() {
    try {
   
  

          InputStream inputStream;//接收字节输入流
           InputStreamReader inputStreamReader;//将字节输入流转换成字符输入流
           BufferedReader bufferedReader;//为字符输入流加缓冲
           FileOutputStream fileOutputStream;//字节输出流
           OutputStreamWriter outputStreamWriter;//将字节输出流转换成字符输出流


           URL wangyi = new URL("http://www.runoob.com/");
           inputStream = wangyi.openStream();
           inputStreamReader = new InputStreamReader(inputStream, "utf-8");
           bufferedReader = new BufferedReader(inputStreamReader);
           String s;
           File dest = new File("src/test/wangyi.txt");
           fileOutputStream = new FileOutputStream(dest);
           outputStreamWriter = new OutputStreamWriter(fileOutputStream, "utf-8");
           
    String regex = "<a[\\s]+href[\\s]*=[\\s]*\"([^<\"]+)\"";  
    //String regex = "[^.]";  
    Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
           
           while ((s = bufferedReader.readLine()) != null) {
            System.out.println(s);
           
    Matcher m = p.matcher(s);  
   
    StringBuffer ret = new StringBuffer();  
    while(m.find())  
    {  
       ret.append(m.group(1));  
    }  
     
    String url = ret.toString();  
    System.out.println("===="+url);


               outputStreamWriter.write(s);
           }
           


 




           outputStreamWriter.close();
           fileOutputStream.close();
           bufferedReader.close();
           inputStreamReader.close();
           inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
 
    }
    
    /** 
     *  
     * @param str 要截取的字符串 
     * @param bytes 截取的字节数 
     * @return 
     * @throws UnsupportedEncodingException 
     */  
    public static String subStringByBytes(String str, int bytes,String charSetName){  
        String subAfter = str.substring(0, bytes);  
        int temp = bytes;  
        try {  
            //直到截取的字符串的字节数  和   需要的 截取的字节数相等位为止  
            while(bytes < subAfter.getBytes(charSetName).length){  
                subAfter = subAfter.substring(0,--temp );  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return subAfter;  
    }  
    /** 
     *  
     * @return 当前系统的编码格式  
     */  
    public static String getSystemEncode() {  
        System.getProperties().list(System.out);// 得到当前的系统属性。并将属性列表输出到控制台  
        String encoding = System.getProperty("file.encoding");  
        System.out.println("Encoding:" + encoding);  
        return encoding;  
    }
    
    public static void way_3(){
   
        final int count = 200;
        new Thread(){
            public void run() {
                for(int i = 1; i <= count ;i=i+2) {
                    // 奇数,起始值为1,增长步长为2
                    try {
                        System.out.println("奇数线程:\t" + i);
                        // 暂停0.5秒
                        sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
        }.start();
        new Thread(){
            public void run() {
                for(int i = 0; i <= count ;i=i+2) {
                    // 偶数,起始值为0,增长步长为2
                    try {
                        System.out.println("偶数线程:\t" + i);
                        // 暂停0.5秒
                        sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
        }.start();
    }




===================================================


package test;


//要打印的资源
public class Num {
    int ji=3;
    int ou=2;
    boolean flag = false; //两个线程,交替执行的一个标志
}



=====================================================


package test;


public class PrintMain {
    public static void main(String[] args) {
        Num num = new Num(); //声明一个资源


        PrintQi pQi = new PrintQi(num);
        PrintOu pOu = new PrintOu(num);


        Thread aThread = new Thread(pQi);
        Thread bThread = new Thread(pOu);


        aThread.start();
        bThread.start();
    }
}



=================================================================

package test;


//打印偶数的线程
public class PrintOu implements Runnable{
    Num num;
    public PrintOu(Num num) {
        this.num = num;
    }
    public void run()
    {   
    int i = 1;
        while(i<=100)
        {
       
            synchronized (num)/* 必须要用一把锁对象,这个对象是num*/ {
                if(!num.flag) {
                    try {
                        num.wait();  //操作wait()函数的必须和锁是同一个
                    } catch (Exception e) {
                    e.printStackTrace();
                    }
                }   
                else {
               
                    System.out.println("2的倍数====="+num.ou * i);
                    i++;
                    try {
num.wait(5L);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
                    num.flag = false;
                    num.notify();
                }
            }
        }
    }
}

========================================================================


package test;


//打印奇数的线程
public class PrintQi implements Runnable{
    Num num ;
    public PrintQi(Num num)
    {
        this.num = num;
    }
    public void run()
    {   
    int i = 1;
        while(i <= 100)
        {   
            synchronized (num) {
                if(num.flag)
                {
                    try {
                        num.wait();
                    } catch (Exception e) {
                    }
                }
                else {
               
                    System.out.println("3的倍数-----"+num.ji * i);
                    i++;
                    try {
num.wait(5L);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
                    num.flag = true;
                    num.notify();
                }
            }
        }
    }
}v













原创粉丝点击