• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#語法學習線程(Thread)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2008/9/11
 * Time: 下午 02:36
 * 
 
*/
using System;
using System.Threading;
class SingleThread
{
    
static void Main(string [] args)
    {
        SingleThread st 
=new SingleThread();
        Thread th 
=new Thread(new ThreadStart(st.SayHello));
        th.Start();
    }
    
public void SayHello()
    {
        Console.WriteLine(
"Hello from a single thread.");
    }
}

/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2008/9/11
 * Time: 下午 02:41
 * 
 
*/

using System;
using System.Threading;
class SyncData
{
    
int index=0;
    
string[] comment=new string[]{"","","","","","","","","","","十一","十二","十三","十四","十五","十六","十七","十八","十九","二十"}; 
    
public string GetNetComment()
    {
        
lock(this)
        {
            
if(index<comment.Length)
            {
                
return comment[index++];
            }
            
else
            {
                
return "empty";
            }
        }
    }
}
class Synchronization
{
    SyncData sdat
=new SyncData();
    
public void GetComments()
    {
        
string comment;
        
do
        {
            comment
=sdat.GetNetComment();
            Console.WriteLine(
"Current Thread:{0},comment:{1}",Thread.CurrentThread.Name,comment);
        }
while(comment!="empty");
    }
    
    
static void Main(string[] args)
    {
        Synchronization sync
=new Synchronization();
        Thread t1
=new Thread(new ThreadStart(sync.GetComments));
        Thread t2
=new Thread(new ThreadStart(sync.GetComments));
        Thread t3
=new Thread(new ThreadStart(sync.GetComments));
        t1.Name
="Thread 1";
        t2.Name
="Thread 2";
        t3.Name
="Thread 3";
        t1.Start();
        t2.Start();
        t3.Start();
    }
}

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#调用C++编写的COMDLL发布时间:2022-07-13
下一篇:
C#获取网页源代码发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap