zoukankan      html  css  js  c++  java
  • C#語法學習線程(Thread)

    /*
     * 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();
        }
    }

    申明

    非源创博文中的内容均收集自网上,若有侵权之处,请及时联络,我会在第一时间内删除.再次说声抱歉!!!

    博文欢迎转载,但请给出原文连接。

  • 相关阅读:
    Python学习系列之类与对象(二十三)
    面向过程和面向对象的异同点
    js 数值精确运算使用math.js
    js实现复制 、剪切功能-clipboard.min.js 示例
    css div嵌套层中button的margin-top不起作用解决方法
    IPhone中H5页面用on绑定click无效的解决方法
    This is a good start.
    element之input输入搜索联想框
    vue + element-ui 国际化实现
    async/await 处理多个网络请求同步问题
  • 原文地址:https://www.cnblogs.com/Athrun/p/1289128.html
Copyright © 2011-2022 走看看