线程和线程池

先上一段简单封装的Thread Class,其实主要是依靠“std::tr1::bind”和“std::tr1::function”来实现的(主要是c++11和boost的内容),以后再作详细解释为什么这个类要这样写比较合适吧以及程序的优化,比如互斥锁其实可以使用std::lock_guard来管理互斥锁。


#include <thread>
#include <iostream>
#include <ostream>
#include <fstream>
#include <mutex>
#include <time.h>



class FileOperation {
public:
	FileOperation();
	~FileOperation();
private:
	std::mutex m_mutex;
public:
	 void Read();
	 void Write();
};

FileOperation::FileOperation(){

}

FileOperation::~FileOperation(){

}

void FileOperation::Read(){
	
	while (true){
		
		m_mutex.lock();

		char *buffer = new char[256];
		std::ifstream ifs("Read.txt");
		if(ifs.is_open()){
			
			while (!ifs.eof()){
				ifs.getline(buffer,100);
				std::cout<<buffer;
			}

			std::cout<<std::endl;
		}

		ifs.close();
		delete[] buffer;
		
		std::chrono::milliseconds timespan(5);
		std::this_thread::sleep_for(timespan);

		m_mutex.unlock();
	}
}

 void FileOperation::Write(){
	
	srand((unsigned int)(time)(NULL));
	while (true){
		
		m_mutex.lock();
	
		std::ofstream of("Read.txt");
	
		if(of.is_open()){
			// of.clear();
			of<< rand()%10000<<std::endl;	
		}

		of.close();
		std::chrono::milliseconds timespan(5);
		std::this_thread::sleep_for(timespan);

		m_mutex.unlock();
	}
}


class ThreadingClass {

	typedef std::tr1::function< void(void) > ThreadFunctionCallBack;

public:
	ThreadingClass(ThreadFunctionCallBack m)
		:m_c(m),
		 m_EndTheading(true)
	{}

	~ThreadingClass();

private:
	ThreadFunctionCallBack m_c; 
	bool m_EndTheading;
	const unsigned int& ThreadingID() const; 

public:
	void RunThreading();
	void StopThreading();
};

ThreadingClass::~ThreadingClass(){
	
}

const unsigned int& ThreadingClass::ThreadingID() const {
	return 1;
}

void ThreadingClass::RunThreading() {
	std::thread m_threading(m_c);
	m_threading.detach();
}

void ThreadingClass::StopThreading(){
	
	// m_threading.detach();
}

int main() {
	
	FileOperation f;
	ThreadingClass m_1(std::tr1::bind(&FileOperation::Read, &f));
	ThreadingClass m_2(std::tr1::bind(&FileOperation::Write,&f));
	
	m_1.RunThreading();
	m_2.RunThreading();
	
	std::cin.get();
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java中,线程是程序执行的基本单元,用于并发执行任务。每个线程都有自己的生命周期,包括创建、运行、阻塞和终止。线程的创建可以通过继承Thread类或实现Runnable接口来实现。 线程池则是Java中一种高效的线程管理机制,它预先创建一定数量的工作线程,并在需要执行任务时从线程池中获取线程进行处理,当任务完成后,线程会返回到线程池等待下一次调度,而不是立即结束。这样可以避免频繁地创建和销毁线程带来的开销,提高系统的性能和资源利用率。 以下是Java线程线程池的一些关键点: 1. **线程创建**: - **继承Thread类**:创建自定义线程类并重写run()方法。 - **实现Runnable接口**:创建Runnable接口的实现类,提供run()方法,然后用Thread构造函数创建Thread实例。 2. **线程状态**: - 新建(New):线程对象被创建但还未启动。 - 运行(Runnable):线程正在执行run()方法。 - 阻塞(Blocked):线程因某个条件而暂停,如I/O操作等待数据。 - 等待(Waiting):线程在调用wait()方法后,进入等待状态,直到被其他线程唤醒。 - 守护(Terminated):非守护线程完成或主线程结束,守护线程自动退出。 3. **线程池组件**: - ExecutorService:线程池的核心接口,提供了提交任务和控制线程的方法。 - ThreadPoolExecutor:实现了ExecutorService,包含核心线程数、最大线程数、任务队列等配置。 - ScheduledThreadPoolExecutor:支持定时和周期性任务。 4. **线程池的优势**: - **资源复用**:减少线程创建和销毁的开销。 - **线程管理和调度**:灵活设置线程数量、线程优先级和任务执行策略。 - **避免死锁**:由于任务有顺序地等待特定资源,减少了死锁的可能性。 - **可扩展性**:随着任务增加,线程池可以根据需要动态调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值