java 实现定时器修改

Java 定时器优化实践
本文介绍了如何在Java中实现定时任务的修改与优化,包括使用ScheduledExecutorService进行定时任务的调度,以及如何动态调整任务执行间隔,确保系统的高效运行。
package test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileOutputStream;

public class CustsmTimerTask {

	/**
	 * Description: 从本地上传文件到共享目录
	 * 
	 * @Version1.0 Sep 25, 2009 3:49:00 PM
	 * @param remoteUrl
	 *            共享文件目录
	 * @param localFilePath
	 *            本地文件绝对路径
	 */
	public boolean smbPut(String remoteUrl, String localFilePath) {
		InputStream in = null;
		OutputStream out = null;
		try {
			File localFile = new File(localFilePath);
			String fileName = localFile.getName();
			SmbFile remoteFile = new SmbFile(remoteUrl + "/" + fileName);
			in = new BufferedInputStream(new FileInputStream(localFile));
			out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
			byte[] buffer = new byte[1024];
			while (in.read(buffer) != -1) {
				out.write(buffer);
				buffer = new byte[1024];
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			try {
				out.close();
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	public static void main(String[] args) {
		
		Runnable runnable = new Runnable() {
			public void run() {
				CustsmTimerTask  cstt=new CustsmTimerTask(); 
				boolean a=cstt.smbPut("smb://administrator:'@10.101.238.70/共享", "d://test/aa.csv") ; 
				System.out.println(a);
			}
		};
		ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
		// 第二个参数为首次执行的延时时间,第三个参数为定时执行的间隔时间
		service.scheduleAtFixedRate(runnable, 1, 1440, TimeUnit.MINUTES);
		
	}
	
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值