JAVA多线程1

package  job;

import  java.io.FileWriter;
import  java.io.IOException;
import  java.util.Date;
import  java.text.SimpleDateFormat;

public   class  MainJob  extends  Thread {
    
private  String path  =   null ;

    
public  MainJob(String filePath) {
        
this .path  =  filePath;
    }

    
public   void  run() {
        
while  ( true ) {
            
try  {
                write(
" Write  "   +  ( new  SimpleDateFormat( " yyyyMMddHHmmss " )).format(( new  Date())));
                Thread.sleep(
3000L );
            } 
catch  (InterruptedException e) {
                utils.Log.add(
this ).error(e.getMessage());
            }
        }
    }

    
private   void  write(String msg) {
        
try  {
            String path 
=  String.format( " %slogs\\msg.txt " this .path);
            FileWriter fw 
=   new  FileWriter(path,  true );
            fw.write(String.format(
" %s - %s\r\n " , ( new  SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " ))
                    .format((
new  Date())), msg));
            fw.flush();
            fw.close();
        } 
catch  (IOException ex) {
            utils.Log.add(
this ).error(ex.getMessage());
        }
    }
}

在JSP中调用
<%
    MainJob job = new MainJob(this.getServletContext().getRealPath("/"));
    job.start();
%>




 

你可能感兴趣的:(java多线程)