首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

求 c# 和 java 的线程的用法 和语法!该怎么处理

2012-02-01 
求c#和 java 的线程的用法 和语法!!请教高手门了~~~[解决办法]Java codepublic class MyThread extends Th

求 c# 和 java 的线程的用法 和语法!!
请教高手门了~~~

[解决办法]

Java code
public class MyThread extends Thread { int count= 1, number; public MyThread(int num) {  number = num;  System.out.println("创建线程 " + number); } public void run() {  while(true) {   System.out.println("线程 " + number + ":计数 " + count);   if(++count== 6) return;  } } public static void main(String args[]) {  for(int i = 0; i 〈 5; i++) new MyThread(i+1).start(); }}
[解决办法]
C# code
using System;using System.Threading;using System.Security.Permissions;public class ThreadWork {public static void DoWork() {try {for(int i=0; i<100; i++) {                Console.WriteLine("Thread - working.");                Thread.Sleep(100);            }        }catch(ThreadAbortException e) {            Console.WriteLine("Thread - caught ThreadAbortException - resetting.");            Console.WriteLine("Exception message: {0}", e.Message);            Thread.ResetAbort();        }        Console.WriteLine("Thread - still alive and working.");        Thread.Sleep(1000);        Console.WriteLine("Thread - finished working.");    }}class ThreadAbortTest {public static void Main() {        ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork);        Thread myThread = new Thread(myThreadDelegate);        myThread.Start();        Thread.Sleep(100);        Console.WriteLine("Main - aborting my thread.");        myThread.Abort();        myThread.Join();        Console.WriteLine("Main ending.");    }}
[解决办法]
c# 可以直接是一个方法来调用,委托
Java 必须要写一个类来着

热点排行