C#多线程的同步与通信
- 格式:pdf
- 大小:74.54 KB
- 文档页数:3


第29卷第5期 技术 与 创 新 管 理 2008年9月TECHNOLOGY AND INNOVATION MANAGEMENT Vo1.29 No.5 Sep.2008 【技术应用与研究】 Java与C#的多线程能力 甘 凯 (西安科技大学计算机学院,陕西西安710054) 摘要:Iava与C#是现如今网络开发的两大主要平台,而多线程编程则是进行并行处理计算中的基础,本文主 要阐述了在Java与c#开发平台下如何进行多线程编程的方法及注意事项,并且比较和分析两种开发平台下进 行多线程编程的差异。 关键词:多线程;并行处理;Java;C# . 中图分类号:TP 311.52 文献标识码:A 文章编号:1672-7312(2008)05-0510-03 Multi-threaded Programming of Java and C{}} GAN KaLi (College ofComputer Science,Xi an Unwersity ofScience and Technology, an 710054,China) Abstract:Java and C#are the two important Develop Studioes of the netwo ̄development now.Multi—threaded Program— ruing is the foundation of Parallel Processing。This article writes about the methods of Multi—threaded Programming in the Java and c#Develop Studioes as well as matters which need to be paid attention to.The article also compares and analyses the diferences of the Multi—threaded Programming in the Java and C#Develop Studioes. Key words:multi—threaded;parallel processing;java;C# 0前言 1线程的创建和运行 线程是允许进行并行计算的一个抽象概念:在 另一个线程完成计算任务的同时,一个线程可以对 图像进行更新,第二个线程可以同时处理同一个进 程发出的两个网络请求。 从概念上讲,线程提供了一种在一个软件中并 行执行代码的方式——每个线程都“同时”在一个 共享的内存空间中执行指令,(当然是在一个处理 器上,这是通过处于运行状态的线程的交替执行完 成的。),因此,每个线程都可以访问一个程序内的 数据结构。 由于这种原因,多线程编程的难度就可 想而知了,因为一个程序内有许多不同的线程需要 安全地共享数据。 Java在java.1ang.Thread和java.1ang.Runnable 类中提供了大部分的线程功能。创建一个线程非常 简单,就是扩展Thread类,并调用start()。通过创 建一个执行Runnable()的类,并将该类作为参数传 递给Thread(),也可以定义一个线程。 下面这个简单的Java程序,其中有2个线程同 时在从1数到5,并将结果打印出来。 public class ThreadingExample extends Object{ public static void main(String args[]){ Thread[]t}lreads=Hew Thread[2]; for(int count=1;count<=threads.1ength; count){ 收稿日期:2008—03—27 作者简介:甘凯(1976一),男,陕西西安人,助教,
Qt多线程同步与通信
Qt 多线程同步与通信
1 多线程同步
Qt提供了以下⼏个类来完成这⼀点:QMutex、QMutexLocker、QSemphore、QWaitCondition。
当然可能还包含QReadWriteLocker、QReadLocker、QWriteLocker,但线程同步是应⽤很少,这⾥只做简单的讲解!
QMutex、QMutexLocker
QMutex类提供了⼀个保护⼀段临界区代码的⽅法,他每次只允许⼀个线程访问这段临界区代码。QMutex::lock()函数⽤来锁住互斥量,如果
互斥量处于解锁状态,当前线程就会⽴即抓住并锁定它;否则当前线程就会被阻塞,直到持有这个互斥量的线程对其解锁。线程调⽤lock()
函数后就会持有这个互斥量直到调⽤unlock()操作为⽌。QMutex还提供了⼀个tryLock()函数,如果互斥量已被锁定,就⽴即返回。
在问题出来了,如果要在stop()函数中使⽤mutex进⾏互斥操作,但unlock()操作写在那⾥?unlock()操作却不得不再return之后,从⽽导致
unlock()操作永远也⽆法执⾏...
Qt提供了QMutexLocker类何以简化互斥量的处理,它在构造函数中接受⼀个QMutex对象作为参数并将其锁定,在析构函数中解锁这个互斥
量。
bool Thread::stop()
{
QMutexLocker locker(&mutex);
m_stop = true;
return m_stop;
}
QReadWriteLocker、QReadLocker、QWriteLocker
下⾯是⼀段对QReadWriteLocker类的对象进⾏,读写锁的操作,⽐较简单,这⾥也不多做讲解了,⾃⼰看吧
MyData data;
QReadWriteLock lock;
void ReaderThread::run()
{
...
lock.lockForRead();
C#多线程线程嵌套调⽤问题
线程嵌套指的是:线程A的执⾏代码启动了线程B,线程B的执⾏代码⼜启动了线程C。
我原本以为线程A被Abort后,线程B会⾃动被Abort,但是我⼤错特错了。
在这种场景下,线程的管理就⾮常重要了。
线程A被Abort后线程B是不会被他的⽗线程Abort的,除⾮你强制在线程A中Abort线程B。
在线程A接收到Abort命令后(catch(ThreadAbortException)),Abort线程B即可。
当然,如果你的需求不是在线程A被Abort后,不Abort线程B,那么也可以不Abort线程B,但是这样就放养了线程B,不受管制。
下⾯是⼀个例⼦:
t1启动线程t2,t2启动了t3.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace InnerThread
{
class Program
{
static void Main(string[] args)
{
Thread t1 = new Thread(new ThreadStart(DoWork1));
= "T1";
t1.Start();
Thread.Sleep(5000);
t1.Abort();
Console.ReadKey();
}
static void DoWork1()
{
Thread t2 = null;
try
{
t2 = new Thread(new ThreadStart(DoWork2));
= "T2";
t2.Start();
while (true)
{
Console.WriteLine("t1 is working");
Thread.Sleep(500);
}
}
catch (ThreadAbortException)
C#多线程代码⽰例
using System;
using System.Threading;
namespace MultiThreadDemo
{
class Program
{
public static void threadfunc()
{
try
{
Console.WriteLine("Child thread starts");
Thread.Sleep(1000);
}
catch (ThreadAbortException / *ex* /)
{
Console.WriteLine("Thread abort!");
}
}
static void Main(string[] args)
{
ThreadStart func = new ThreadStart(threadfunc);
Thread th = new Thread(func);
th.Start();
Thread.Sleep(500);
th.Abort();
Console.ReadLine();
}
}
}
ThreadStart ⽆需传参给线程函数时
using System;
using System.Threading;
// The ThreadWithState class contains the information needed for
// a task, and the method that executes the task.
//
public class ThreadWithState
{
// State information used in the task.
private string boilerplate;
private int numberValue;
// The constructor obtains the state information.
public ThreadWithState(string text, int number)