在华清远见培训日记#day29

作业:

第一题:

pthread_mutex_t m1;
pthread_mutex_t m2;

void *task2(void *arg){
		pthread_mutex_lock(&m2);
		int fd1=open("./work.txt",O_RDONLY);
		char buf[64]={0};
		int res=read(fd1,buf,30);
		printf("%s\n",buf);
		//memset(buf,0,sizeof(buf));
		close(fd1);
		pthread_mutex_unlock(&m1);
}

int main(int argc, const char *argv[])
{
	pthread_t id2;
	pthread_create(&id2,0,task2,0);
	
	pthread_mutex_init(&m1,0);
	pthread_mutex_init(&m2,0);
	pthread_mutex_lock(&m2);

	pthread_mutex_lock(&m1);
	int fd=open("./work.txt",O_WRONLY|O_CREAT|O_TRUNC,0664);
	char str[64]="helloworldilovechinaicandomoreicandobetter";
	write(fd,str,30);
	close(fd);
	pthread_mutex_unlock(&m2);

	pthread_join(id2,0);

	return 0;
}

第二题:

pthread_mutex_t m,m1,m2,m3,m4;

void *task(void *arg){
	while(1){	
		pthread_mutex_lock(&m1);
		printf("2\n");
		sleep(1);
		pthread_mutex_unlock(&m2);
	}
}

void *task1(void *arg){
	while(1){
		pthread_mutex_lock(&m2);
		printf("3\n");
		sleep(1);
		pthread_mutex_unlock(&m3);
	}
}

void *task2(void *arg){
	while(1){
		pthread_mutex_lock(&m3);
		printf("4\n");
		sleep(1);
		pthread_mutex_unlock(&m4);
	}
}

void *task3(void *arg){
	while(1){
		pthread_mutex_lock(&m4);
		printf("5\n");
		sleep(1);
		pthread_mutex_unlock(&m);
	}
}

int main(int argc, const char *argv[])
{
	pthread_t id,id1,id2,id3;

	pthread_mutex_init(&m,0);
	pthread_mutex_init(&m1,0);
	pthread_mutex_lock(&m1);
	pthread_mutex_init(&m2,0);
	pthread_mutex_lock(&m2);
	pthread_mutex_init(&m3,0);
	pthread_mutex_lock(&m3);
	pthread_mutex_init(&m4,0);
	pthread_mutex_lock(&m4);

	pthread_create(&id,0,task,0);
	pthread_create(&id1,0,task1,0);
	pthread_create(&id2,0,task2,0);
	pthread_create(&id3,0,task3,0);
	
	pthread_detach(id);
	pthread_detach(id1);
	pthread_detach(id2);
	pthread_detach(id3);

	while(1){
		pthread_mutex_lock(&m);
		printf("1\n");
		sleep(1);
		pthread_mutex_unlock(&m1);
	}
	return 0;
}

思维导图:

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Android开发中,`TIMELINE.DiffTimeReloadTask`通常用于异步加载和更新Timeline的数据。`onReloading(Day day)`方法可能是一个回调,当特定日期(Day)的数据需要重新加载时会被调用。`Day`可能是自定义的时间线条目,代表一天内的事件。 关于`DiffTimeReloadTask.DAY.onReloading`的具体实现,由于没有直接的代码片段可用,我们可以推测它的大概结构。这个方法可能会执行以下操作: 1. **检查缓存**[^1]: ```java List<TimelineItem> itemsFromCache = cache.get(day); ``` 2. **如果缓存中有数据,直接返回**: ```java if (itemsFromCache != null) { return itemsFromCache; } ``` 3. **开始网络请求或数据库查询**: ```java timelineService.getDataForDay(day, new Callback<List<TimelineItem>>() { @Override public void onResponse(List<TimelineItem> items) { // 更新缓存并同步到视图 updateCache(day, items); onItemsLoaded(items); } @Override public void onFailure(Exception e) { // 处理加载失败 handleLoadFailure(e); } }); ``` `getItems()`方法可能会在`onResponse`里被用来获取实际的事件项列表: ```java private void onItemsLoaded(List<TimelineItem> items) { DiffResult<TimelineItem> result = DiffUtil.calculateDiff(new TimelineItemComparator(), items, oldItems); DAY.setItems(result.newItems, result.changedItems, result.deletedItems); DAY.notifyDataSetChanged(); } ``` 这里的`TimelineItemComparator`用于比较新旧数据以决定哪些项目需要添加、更改或删除。` DiffResult`包含了这些信息,`setItems`方法则负责更新UI。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值