work方法 java_Java WorkQueue.WorkItem方法代碼示例

import com.facebook.internal.WorkQueue; //導入方法依賴的package包/類

@Override

public void run() {

// Each iteration runs a random action against the WorkQueue.

if (iterationIndex++ < iterationCount) {

final int sleepWeight = 80;

final int trackThisWeight = 10;

final int prioritizeTrackedWeight = 6;

final int validateWeight = 2;

int weight = 0;

final int n = random.nextInt(sleepWeight + trackThisWeight + prioritizeTrackedWeight + validateWeight);

WorkQueue.WorkItem workItem = manager.addActiveWorkItem(this);

if (n < (weight += sleepWeight)) {

// Sleep

try {

Thread.sleep(n/4);

} catch (InterruptedException e) {

}

} else if (n < (weight += trackThisWeight)) {

// Track this work item to activate later

synchronized (tracked) {

tracked.add(workItem);

}

} else if (n < (weight += prioritizeTrackedWeight)) {

// Background all pending items, prioritize tracked items, and clear tracked list

ArrayList items = new ArrayList();

synchronized (tracked) {

items.addAll(tracked);

tracked.clear();

}

for (WorkQueue.WorkItem item : items) {

item.moveToFront();

}

} else {

// Validate

manager.validate();

}

} else {

// Also have all threads validate once they are done.

manager.validate();

synchronized (this) {

isDone = true;

this.notifyAll();

}

}

}

为下面这段代码写一个makefile文件,#include <linux/init.h> #include <linux/module.h> #include <linux/workqueue.h> #include <linux/sched.h> #include <linux/delay.h> #include <linux/timekeeping.h> MODULE_LICENSE("GPL"); static struct workqueue_struct* workqueue; static struct work_struct work1; static struct delayed_work work2; static int times; module_param(times, int, 5); int get_time(char* buff, int len) { struct timespec64 ts; struct tm tm_res; char time_string[40]; ktime_get_real_ts64(&ts); time64_to_tm(ts.tv_sec, 0, &tm_res); buff=tm_res.tm_year + 1900, tm_res.tm_mon + 1, tm_res.tm_mday,tm_res.tm_hour, tm_res.tm_min, tm_res.tm_sec; return 0; } static void real_time_work(struct work_struct* work) { char buff[40]; get_time(buff, sizeof(buff)); printk(KERN_INFO "%s\n",buff); } static void delayed_work(struct work_struct* work) { char buff[40]; get_time(buff, sizeof(buff)); printk(KERN_INFO "this is a delay work :%s\n", buff); } static int __init module_init(void) { printk(KERN_INFO "Initializing workqueue module.\n"); workqueue = create_workqueue("workqueue"); INIT_WORK(&work1, real_time_work); INIT_DELAYED_WORK(&work2, delayed_work); queue_work(workqueue, &work1); queue_delayed_work(workqueue, &work2, msecs_to_jiffies(10000)); return 0; } static void __exit module_exit(void) { printk(KERN_INFO "Exiting workqueue module.\n"); flush_workqueue(workqueue); destroy_workqueue(workqueue); } module_init(module_init); module_exit(module_exit);要求. 把加载、卸载内核模块以 install/uninstall 写入 Makefile 文件中。
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值