×

多线程程序例子

多线程程序例子(谁有ucos多线程程序设计的例子)

admin admin 发表于2023-11-26 10:18:49 浏览52 评论0

抢沙发发表评论

大家好,多线程程序例子相信很多的网友都不是很明白,包括谁有ucos多线程程序设计的例子也是一样,不过没有关系,接下来就来为大家分享关于多线程程序例子和谁有ucos多线程程序设计的例子的一些知识点,大家可以关注收藏,免得下次来找不到哦,下面我们开始吧!

本文目录

谁有ucos多线程程序设计的例子

这里有几个实例: 多线程的概念在此就不多说了,打个通熟易懂比方,把自己看做一个进程,做的每件事都看做为线程,自己可以同时玩魔兽和听歌,那么玩魔兽和听歌就是两个线程,为多线程。 java是少数集中支持多线程的语言之一,大多数的语言只能运行一个程序块,无法同时运行不同的程序块,而java则弥补了这个缺陷。 举个公司项目开发遇到的一个简单例子,用户上传压缩文件到服务器后,要对该压缩包进行两个操作,一是将该压缩包复制到指定目录,一是将该压缩包解压到另一指定目录,最终响应用户提示文件上传成功。如果压缩包很大的话,上传后进行的复制和解压功能也会占用很长时间,用户就会等待很长的时间。其实复制和解压的功能和用户操作没有直接关系,完全可以独立出来,其解决的思路如下: 当用户上传压缩文件完毕之后,我们立即创建两个线程,一是复制压缩文件的线程;二是解压压缩文件的线程。我们可以通过线程的构造方法把文件的信息传递给相应的线程,当我们启动两线程的start方法后,我们就不必再关心其复制和解压的操作,而是直接响应用户,这样用户明显感觉操作变快,而复制和解压的操作仍在后台偷偷的进行着。 实现多线程的方法有两个,一是继承Thread,二是实现接口Runnable。二者的区别不多说,继承只能单继承,而接口可以实现多个,故本人更倾向使用后者。 下面把代码模型贴出来,供大家参考参考: args) { Long begin = System.currentTimeMillis(); // 上传文件 UploadFile uploadFile = new UploadFile(); File file = uploadFile.uploadFileMethod(); // 给线程传递参数 CoppyFile coppyFile = new CoppyFile(file); UnZipFile unZipFile = new UnZipFile(file); // 创建线程 Thread coppyThread = new Thread(coppyFile); Thread unZipThread = new Thread(unZipFile); // 启动线程 coppyThread.start(); unZipThread.start(); Long end = System.currentTimeMillis(); // 响应用户请求 System.out.println("恭喜,文件上传成功,耗时:" + (end - begin) + "毫秒"); } } /** * 上传文件类 * * @author yangjd * */ class UploadFile { // 文件上传 public File uploadFileMethod() { File file = new File("filePath"); System.out.println("文件上传完毕"); return file; } } /** * 复制文件类 * * @author yangjd * */ class CoppyFile implements Runnable { private File file; public CoppyFile(File file) { this.file = file; } @Override public void run() { coppyFileMethod(file); } // 文件复制 public void coppyFileMethod(File file) { // 睡眠15秒钟 try { Thread.sleep(15000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("文件复制完毕"); } } /** * 解压文件类 * * @author yangjd * */ class UnZipFile implements Runnable { private File file; public UnZipFile(File file) { this.file = file; } @Override public void run() { unZipFileMethod(file); } // 文件解压 public void unZipFileMethod(File file) { // 睡眠10秒钟 try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("文件解压完毕"); } }

求一个windows下的多线程的例子,不要linux下的!

下面是一个用VC编的最简单的多线程例子,希望对你有帮助。1. 建立一个基于对话框的工程MultiThread1,在对话框IDD_MULTITHREAD1_DIALOG中加入两个按钮和一个框,两个按钮的ID分别是IDC_START,IDC_STOP ,标题分别为“启动”,“停止”,IDC_STOP的属性选中Disabled;框的ID为IDC_TIME ,属性选中Read-only; 2. 在MultiThread1Dlg.h文件中添加线程函数声明:void ThreadFunc();注意,线程函数的声明应在类CMultiThread1Dlg的外部。 在类CMultiThread1Dlg内部添加protected型变量:HANDLE hThread;DWORD ThreadID;分别代表线程的句柄和ID。 3. 在MultiThread1Dlg.cpp文件中添加全局变量m_bRun :BOOL m_bRun;m_bRun 代表线程是否正在运行。编写线程函数:void ThreadFunc(){CTime time;CString strTime;m_bRun=TRUE;while(m_bRun){time=CTime::GetCurrentTime();strTime=time.Format("%H:%M:%S");::SetDlgItemText(AfxGetMainWnd()-》m_hWnd,IDC_TIME,strTime);Sleep(1000);}}该线程函数没有参数,也不返回函数值。只要m_bRun为TRUE,线程一直运行。双击IDC_START按钮,完成该按钮的消息函数:void CMultiThread1Dlg::OnStart(){// TODO: Add your control notification handler code herehThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadFunc,NULL,0,&ThreadID);GetDlgItem(IDC_START)-》EnableWindow(FALSE);GetDlgItem(IDC_STOP)-》EnableWindow(TRUE);}双击IDC_STOP按钮,完成该按钮的消息函数:void CMultiThread1Dlg::OnStop(){// TODO: Add your control notification handler code herem_bRun=FALSE;GetDlgItem(IDC_START)-》EnableWindow(TRUE);GetDlgItem(IDC_STOP)-》EnableWindow(FALSE);}

求一个java多线程的简单例子,简单点

首先你得知道什么是进程,任务管理器有进程,一个进程可以有多个线程。eg:你开了个360这个任务进程,在这个进程下你即可以进行电脑体检,也可以清扫垃圾,同步进行。

java 多线程的例子

多线程实际上就是多个线程同时运行,至于那个先完成是不能确定的。

* @author Rollen-Holt 实现Runnable接口

* */

class hello implements Runnable {

   public hello() {

   }

   public hello(String name) {

       this.name = name;

   }

   public void run() {

       for (int i = 0; i 《 5; i++) {

           System.out.println(name + "运行     " + i);

       }

   }

   public static void main(String args) {

       hello h1=new hello("线程A");

       Thread demo= new Thread(h1);

       hello h3=new hello("线程B");

       Thread demo1=new Thread(h3);

       demo.start();

       demo1.start();

   }

   private String name;

}

可能运行结果:

java的多线程简单例子

package e;public class A {public static void main(String args) throws Exception{new TestThread().start();for(int i=0;i《10;i++){        Thread.sleep(3000);System.out.println("main");}}}class TestThread extends Thread{public void run(){for(int i=0;i《10;i++){System.out.println("Test");}}}

VB多线程示例!

窗体代码:Private Sub Command1_Click() ’为addnumber过程创建线程 Call CreateNewThread(AddressOf addnumber)End Sub Private Sub Command2_Click() TerminateTh ’终止线程 Unload Me ’结束程序End Sub模块:Private Declare Function CreateThread Lib "kernel32" (ByVal lpThreadAttributes As Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, lpThreadID As Long) As LongPrivate Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As LongPrivate Declare Function SetThreadPriority Lib "kernel32" (ByVal hThread As Long, ByVal nPriority As Long) As LongPrivate Declare Function GetThreadPriority Lib "kernel32" (ByVal hThread As Long) As LongPrivate Declare Function ResumeThread Lib "kernel32" (ByVal hThread As Long) As LongPrivate Declare Function SuspendThread Lib "kernel32" (ByVal hThread As Long) As LongPrivate Declare Function GetCurrentThread Lib "kernel32" () As LongPrivate Declare Function GetCurrentThreadId Lib "kernel32" () As LongPrivate Const MAXLONG = &H7FFFFFFFPrivate Const THREAD_BASE_PRIORITY_IDLE = -15Private Const THREAD_BASE_PRIORITY_LOWRT = 15Private Const THREAD_BASE_PRIORITY_MAX = 2Private Const THREAD_BASE_PRIORITY_MIN = -2Private Const THREAD_PRIORITY_HIGHEST = THREAD_BASE_PRIORITY_MAXPrivate Const THREAD_PRIORITY_LOWEST = THREAD_BASE_PRIORITY_MINPrivate Const THREAD_PRIORITY_ABOVE_NORMAL = (THREAD_PRIORITY_HIGHEST - 1)Private Const THREAD_PRIORITY_BELOW_NORMAL = (THREAD_PRIORITY_LOWEST + 1)Private Const THREAD_PRIORITY_ERROR_RETURN = (MAXLONG)Private Const THREAD_PRIORITY_IDLE = THREAD_BASE_PRIORITY_IDLEPrivate Const THREAD_PRIORITY_NORMAL = 0Private Const THREAD_PRIORITY_TIME_CRITICAL = THREAD_BASE_PRIORITY_LOWRTPrivate Const CREATE_ALWAYS = 2Private Const CREATE_NEW = 1Private Const CREATE_NEW_CONSOLE = &H10Private Const CREATE_NEW_PROCESS_GROUP = &H200Private Const CREATE_NO_WINDOW = &H8000000Private Const CREATE_PROCESS_DEBUG_EVENT = 3Private Const CREATE_SUSPENDED = &H4Private Const CREATE_THREAD_DEBUG_EVENT = 2Public Enum ThreadPriority tpLowest = THREAD_PRIORITY_LOWEST tpBelowNormal = THREAD_PRIORITY_BELOW_NORMAL tpNormal = THREAD_PRIORITY_NORMAL tpAboveNormal = THREAD_PRIORITY_ABOVE_NORMAL tpHighest = THREAD_PRIORITY_HIGHESTEnd EnumPrivate mThreadHandle As Long’示例Function addnumber() As Long Dim i As Long For i = 0 To 100000 Form1.Label1.Caption = i NextEnd Function’创建线程Public Function CreateNewThread(ByVal cFunction As Long) As Long Dim mhandle As Long Dim lpThreadID As Long mhandle = CreateThread(ByVal 0&, ByVal 0&, cFunction, ByVal 0&, 0, lpThreadID) If mhandle 《》 0 Then mThreadHandle = mhandleEnd Function’终止线程Public Function TerminateTh() As Long SuspendThread mThreadHandle TerminateThread mThreadHandle, ByVal 0& TerminateThread lpThreadID, ByVal 0&End Function

编写一个多线程程序,使得3个线程同时运行

才给5分啊~~~~~~难怪大家懒得做。我就贴一下读写者问题的多线程程序吧,希望对你有帮助。(好像是2个线程,太久了懒得看)#include "windows.h"#include 《conio.h》#include 《stdlib.h》#include 《fstream.h》#include 《io.h》#include 《string.h》#include 《stdio.h》#define READER ’R’ //读者#define WRITER ’W’ //写者#define INTE_PER_SEC 1000 //每秒时钟中断次数#define MAX_THREAD_NUM 64 //最大线程数目#define MAX_FILE_NUM 32 //最大数据文件数目#define MAX_STR_LEN 32 //字符串长度int readcount=0; //读者数目 (属临界资源,需以互斥方式访问)int writecount=0; //写者数目 (属临界资源,需以互斥方式访问)//定义3个临界资源CRITICAL_SECTION RP_Write; //临界区控制变量 (用于读者优先)CRITICAL_SECTION cs_Write; //临界区控制变量 (用于写者优先)CRITICAL_SECTION cs_Read; //临界区控制变量 (用于写者优先)//定义线程及其操作行为信息结构struct ThreadInfo{ int serial; //线程序号 char entity; //线程类别(是读者还是写者) double delay; //线程延迟 double persist; //线程读写操作的持续时间};// 读者优先――读者线程void RP_ReaderThread(void *p) //p: 读者线程信息{ //互斥变量 HANDLE h_Mutex; h_Mutex = OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex_for_readcount"); //从参数中获得信息 DWORD wait_for_mutex; //等待互斥变量所有权 DWORD m_delay; //延迟时间 DWORD m_persist; //读文件持续时间 int m_serial; //线程序号 m_serial = ((ThreadInfo*)(p))-》serial; m_delay = (DWORD)((ThreadInfo*)(p))-》delay*INTE_PER_SEC; m_persist = (DWORD)((ThreadInfo*)(p))-》persist*INTE_PER_SEC; Sleep(m_delay); //延迟等待 //输出读进程已发出读请求的提示信息 printf ("Reader thread %d sends the reading require.\n", m_serial); //等待互斥信号,保证对readcount的互斥访问 wait_for_mutex = WaitForSingleObject(h_Mutex,-1); readcount++; if (readcount==1) //第一个读者,等待临界区资源 EnterCriticalSection(&RP_Write); ReleaseMutex(h_Mutex); //释放互斥信号 //输出读进程I 正在临界区中读操作的提示信息 printf("Reader thread %d begins to read file.\n", m_serial); Sleep(m_persist); //退出线程 printf("Reader thread %d finished reading file. \n",m_serial); //等待互斥信号,保证对readcount的互斥访问 wait_for_mutex = WaitForSingleObject(h_Mutex,-1); readcount--; if (readcount==0) LeaveCriticalSection(&RP_Write); ReleaseMutex(h_Mutex); //释放互斥信号}// 读者优先――写者线程void RP_WriterThread(void *p) //p: 写者线程信息{ DWORD m_delay; //延迟时间 DWORD m_persist; //读文件持续时间 int m_serial; //线程序号 //从参数中获取信息m_serial, m_delay, m_persist , m_serial = ((ThreadInfo*)(p))-》serial; m_delay = (DWORD)((ThreadInfo*)(p))-》delay*INTE_PER_SEC; m_persist = (DWORD)((ThreadInfo*)(p))-》persist*INTE_PER_SEC; //延迟等待m_delay秒; Sleep(m_delay); //输出信息"写线程 %d 发出写请求; printf ("Writer thread %d sends the writing require.\n", m_serial); //等待临界资源 EnterCriticalSection(&RP_Write); //输出正在临界区执行写操作的提示信息 //在临界区中写操作,至少持续m_persist秒钟 printf("Writer thread %d begins to write file.\n", m_serial); Sleep(m_persist); printf("Writer thread %d finished writing file. \n",m_serial); //释放临界资源 LeaveCriticalSection(&RP_Write);} // 读者优先――处理函数void ReaderPriority(char *file) //file: 文件名{ DWORD n_thread=0; //保存线程数目变量 DWORD thread_ID; //线程ID DWORD wait_for_all; //保存――等待所有线程结束的返回值――的变量 HANDLE h_Thread; ThreadInfo thread_info; HANDLE h_Mutex=CreateMutex(NULL,FALSE,"mutex_for_readcount"); readcount=0; InitializeCriticalSection(&RP_Write); //初始化临界区 ifstream inFile; inFile.open(file); printf("Reader Priority :\n\n"); while (inFile) { //读入每个读者、写者的信息 inFile》》thread_info.serial; inFile》》thread_info.entity; inFile》》thread_info.delay; inFile》》thread_info.persist; inFile.get(); n_thread++; } n_thread--; //modify for(int j=0;j《(int)(n_thread); j++){ cout《《"threadinfo.entity 《《","《《thread_info.persist《《endl; } for(int i=0; i《(int)(n_thread); i++){ //创建一个不被子进程继承的,使用默认大小堆栈的线程 if( thread_info.entity==’r’) h_Thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)(RP_ReaderThread), &thread_info,0,&thread_ID); // thread_info籍此传递给被创建线程, // 创建标志=0, 表示线程被创建后立即执行 (其它:CREATE_SUSPEND) //thread_ID 存放放回的线程ID标识, 可不用 else h_Thread=CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)(RP_WriterThread), &thread_info,0,&thread_ID); } //等待所有的线程结束 wait_for_all = WaitForMultipleObjects(n_thread, h_Thread, TRUE, -1); printf("All reader and writer have finished operating. \n");}// 写者优先――读者线程void WP_ReaderThread(void *p) //p: 读者线程信息{ DWORD m_delay; //延迟时间 DWORD m_persist; //读文件持续时间 int m_serial; //线程序号 m_serial = ((ThreadInfo*)(p))-》serial; m_delay = (DWORD)((ThreadInfo*)(p))-》delay*INTE_PER_SEC; m_persist = (DWORD)((ThreadInfo*)(p))-》persist*INTE_PER_SEC; Sleep(m_delay); printf ("Reader thread %d sends the reading require.\n", m_serial);//延迟等待//从参数中获取信息m_serial, m_delay, m_persist , //延迟等待m_delay秒;//输出信息"读线程 %d 发出读请求的提示信息; //打开(获取)两个互斥变量 HANDLE h_Mutex1, h_Mutex2; h_Mutex1=OpenMutex(MUTEX_ALL_ACCESS, FALSE, "mutex_for_writecount1");/*由于写者优先,即使临界区中已有读者,也不能马上进入,要等到没有等待写者(在写进程中,当无等待写者释放读者临界区所有权cs_Read)因此,这里需要一个互斥信号量附加控制读者进入过程,这在读者优先算法中是不需要的。*/ DWORD wait_for_mutex1=WaitForSingleObject(h_Mutex1,-1); EnterCriticalSection(&cs_Read);// 用h_mutex2控制对readcount的访问 h_Mutex2 = OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex_for_readcount2"); DWORD wait_for_mutex2 = WaitForSingleObject(h_Mutex2,-1); readcount++; if ( readcount==1) //第一个读者要等待写者写完 EnterCriticalSection(&cs_Write); ReleaseMutex(h_Mutex2); LeaveCriticalSection(&cs_Read); //让其它读者可进入临界区 ReleaseMutex(h_Mutex1); //输出某读者进程在临界区中读的信息;//在临界区中读操作,至少持续m_persist秒钟//输出某读者进程退出临界区的信息; printf("Reader thread %d begins to read file.\n", m_serial); Sleep(m_persist);//退出线程 printf("Reader thread %d finished reading file. \n",m_serial); wait_for_mutex2 = WaitForSingleObject(h_Mutex2,-1); readcount--; if ( readcount==0) //最后一个读者,要唤醒写者 LeaveCriticalSection(&cs_Write); ReleaseMutex(h_Mutex2); }// 写者优先――写者线程void WP_WriterThread(void *p) //p: 写者线程信息{ DWORD m_delay; //延迟时间 DWORD m_persist; //读文件持续时间 int m_serial; //线程序号 //从参数中获取信息m_serial, m_delay, m_persist , m_serial = ((ThreadInfo*)(p))-》serial; m_delay = (DWORD)((ThreadInfo*)(p))-》delay*INTE_PER_SEC; m_persist = (DWORD)((ThreadInfo*)(p))-》persist*INTE_PER_SEC;//延迟等待m_delay秒; Sleep(m_delay);//输出信息"写线程 %d 发出写请求; printf ("Writer thread %d sends the writing require.\n", m_serial);//打开(获取)两个互斥变量h_Mutex3 HANDLE h_Mutex3; h_Mutex3 = OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex_for_writecount3"); DWORD wait_for_mutex3 = WaitForSingleObject(h_Mutex3,-1); writecount++; if (writecount==1) //第一个等待写者,等待读者读完 EnterCriticalSection(&cs_Read); ReleaseMutex(h_Mutex3); EnterCriticalSection(&cs_Write); //进入写着临界区//输出某写者进程在临界区中写的信息;//至少持续m_persist秒钟//输出某写者进程退出临界区的信息; printf("Writer thread %d begins to write file.\n", m_serial); Sleep(m_persist); printf("Writer thread %d finished writing file. \n",m_serial); LeaveCriticalSection(&cs_Write); //写者离开临界区 wait_for_mutex3 = WaitForSingleObject(h_Mutex3,-1); writecount--; if (writecount==0) //无其它等待写者,唤醒第一个读者 LeaveCriticalSection(&cs_Read); ReleaseMutex(h_Mutex3);}// 写者优先――处理函数void WriterPriority(char *file) //file: 文件名{ DWORD n_thread=0, thread_ID, wait_for_all; HANDLE h_Mutex1=CreateMutex(NULL,FALSE,"mutex_for_writecount1"); HANDLE h_Mutex2=CreateMutex(NULL,FALSE,"mutex_for_readcount2"); HANDLE h_Mutex3=CreateMutex(NULL,FALSE,"mutex_for_writecount3");//用CreateMutex创建3个互斥信号量句柄h_mutex1,h_mutex2, h_mutex3;//从数据文件读入信息,创建指定数目的读写进程//读进程处理函数 WP_ReaderThread, 写进程处理函数WP_writerThread HANDLE h_Thread; ThreadInfo thread_info; writecount=0; readcount=0; InitializeCriticalSection(&cs_Write); InitializeCriticalSection(&cs_Read);//初始化临界区 ifstream inFile; inFile.open(file); printf("Write Priority :\n\n"); while (inFile) { //读入每个读者、写者的信息 inFile》》thread_info.serial; inFile》》thread_info.entity; inFile》》thread_info.delay; inFile》》thread_info.persist; inFile.get(); n_thread++; } n_thread--; for(int j=0;j《(int)(n_thread); j++){ cout《《"threadinfo.entity 《《","《《thread_info.persist《《endl; } for(int i=0; i《(int)(n_thread); i++) { //创建一个不被子进程继承的,使用默认大小堆栈的线程 if( thread_info.entity==’r’ ) h_Thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)(WP_ReaderThread), &thread_info,0,&thread_ID); // thread_info籍此传递给被创建线程, // 创建标志=0, 表示线程被创建后立即执行 (其它:CREATE_SUSPEND) //thread_ID 存放放回的线程ID标识, 可不用 else h_Thread=CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)(WP_WriterThread), &thread_info,0,&thread_ID); }//等待所有的线程结束 wait_for_all = WaitForMultipleObjects(n_thread, h_Thread, TRUE, -1); printf("All reader and writer have finished operating. \n");}//////////////////////////////////////////////////////////////主函数int main(int argc, char** argv){ char ch; while (true) { //打印显示信息 printf("***************************************\n"); printf(" 1. Reader Priority \n"); printf(" 2. Writer Priority \n"); printf(" 3. Exit \n"); printf("***************************************\n"); printf("Enter your choice(1,2,or 3): \n"); do { ch = (char)_getch(); } while (ch != ’1’ && ch != ’2’ && ch!=’3’); system("cls"); if(ch==’3’) return 0; else if(ch==’1’) ReaderPriority("thread.dat"); else if(ch==’2’) WriterPriority("thread.dat"); //结束 printf("\nPress any key to continute:"); _getch(); system("cls"); } return 1;}

关于多线程程序例子到此分享完毕,希望能帮助到您。