TAILIEUCHUNG - TCP/IP Sockets in C# Practical Guide for Programmers phần 7

Xây dựng: đường 13-15 Mỗi thể hiện của ThreadExample chứa chuỗi của lời chào của nó. Khởi tạo một thể hiện của Random (): dòng 18 sử dụng để tạo ra một số ngẫu nhiên của thời gian ngủ. cho vòng lặp: dòng 20 Vòng 10 lần. In thread id và dụ | 102 Chapter 4 Beyond the Basics 28 29 30 31 class ThreadExample 32 33 static void Main string args 34 35 MyThreadClass mtc1 new MyThreadClass Hello 36 new Thread new ThreadStart .Start 37 38 MyThreadClass mtc2 new MyThreadClass Aloha 39 new Thread new ThreadStart .Start 40 41 MyThreadClass mtc3 new MyThreadClass Ciao 42 new Thread new ThreadStart .Start 43 44 1. MyThreadClass lines 3-29 In order to pass state to the method we will be running as its own thread we put the method in its own class and pass the state variables in the class constructor. In this case the state is the string greeting to be printed. Constructor lines 13-15 Each instance of ThreadExample contains its own greeting string. Initialize an instance of Random line 18 Used to generate a random number of sleep times. for loop line 20 Loop 10 times. Print the thread id and instance greeting lines 21-22 The static method returns a unique id reference to the thread from which it is called. Suspend thread lines 24-26 After printing its instance s greeting message each thread sleeps for a random amount of time between 0 and 500 milliseconds by calling the static method which takes the number of milliseconds to sleep as a parameter. The 500 call returns a random int between 0 and 500. can be interrupted by another thread in which case ThreadInterruptedException is thrown. Our example does not include an interrupt call so the exception will not happen in this application. Threads 103 2. Main lines 33-43 Each of the three groupings of statements in Main does the following 1 creates a new instance of MyThreadClass with a different greeting string 2 passes the runMyThread method of the new instance to the constructor of Threadstart 3 passes the Threadstart instance to the constructor of Thread and 4 calls the new Thread instance s Start method. Each thread independently

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.