package com.admomo.scrape.util;import java.io.File;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;import com.acwoan.core.orm.DAOUtil;
import com.acwoan.core.util.StdLib;public class GuanliWeibo extends Thread {
long next;
Thread thread = new Thread();
//main方法入口
public static void main(String[] args){
int i = 3000;
Thread thread = new Thread();
GuanliWeibo guanli = new GuanliWeibo();
guanli.run();
try {
while(true){
thread.sleep(i);
System.out.println("main入口 循环调用notifyWait方面!");
guanli.notifyWait();
}
} catch (Exception e) {
// TODO: handle exception
}
}
public void run(){
try {
while (true) {
next = 30;
//同步锁
synchronized(this){
System.out.println("线程进入等待阶段!");
//进入等待阶段,以毫秒计算,等待时间差
thread.wait(next);
// function
uploadSinaWeibo();
}
}
}catch (Exception e) {
// TODO: handle exception
}
}
//启动run方面中的等待线程
public void notifyWait(){
synchronized(this){
thread.notify();
System.out.println("启动run方面中的等待线程");
}
}
}
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;import com.acwoan.core.orm.DAOUtil;
import com.acwoan.core.util.StdLib;public class GuanliWeibo extends Thread {
long next;
Thread thread = new Thread();
//main方法入口
public static void main(String[] args){
int i = 3000;
Thread thread = new Thread();
GuanliWeibo guanli = new GuanliWeibo();
guanli.run();
try {
while(true){
thread.sleep(i);
System.out.println("main入口 循环调用notifyWait方面!");
guanli.notifyWait();
}
} catch (Exception e) {
// TODO: handle exception
}
}
public void run(){
try {
while (true) {
next = 30;
//同步锁
synchronized(this){
System.out.println("线程进入等待阶段!");
//进入等待阶段,以毫秒计算,等待时间差
thread.wait(next);
// function
uploadSinaWeibo();
}
}
}catch (Exception e) {
// TODO: handle exception
}
}
//启动run方面中的等待线程
public void notifyWait(){
synchronized(this){
thread.notify();
System.out.println("启动run方面中的等待线程");
}
}
}
我想实现的功能是这样的:比如从数据库查询一个时间数值出来,和系统时间对比,获取中间时间差,然后thread.sleep(睡眠时间差);在执行后面发送代码。 需要注意的是 数据库中的时间数据不是固定的 随时修改的,这个地方要一个循环包围,如果第二次查询数据库的时候找到一个时间和系统时间更为接近的,那么就要唤醒刚才睡眠的进程,换上最为接近的时间差。 相当于一个定时器 。 定时发送的功能 求接 谢谢各位
int time=3000;
GuanliWeibo guanliWeibo=new GuanliWeibo();
new Thread(guanliWeibo).start();
while(true){
try {
Thread.sleep(time);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("main入口 循环调用notifyWait方面!");
guanliWeibo.notifyWait();
}
}}
package com.admomo.scrape.util;public class GuanliWeibo implements Runnable{
private static final String lockUpload="lockUpload";
private int next=30;
public int getNext() {
return next;
} public void setNext(int next) {
this.next = next;
} @Override
public void run() {
while (true) {
//同步锁
synchronized (lockUpload) {
System.out.println("线程进入等待阶段!");
// 进入等待阶段,以毫秒计算,等待时间差
try {
lockUpload.wait(next);
// function
//uploadSinaWeibo();
} catch (Exception e) {
e.printStackTrace();
}
}
}
} //启动run方面中的等待线程
public void notifyWait() {
synchronized (lockUpload) {
lockUpload.notify();
System.out.println("启动run方面中的等待线程");
}
}
}