010RJPOS:以java任意的类函数、类实例对象函数为执行代码创建子线程

        至此,JVM的开发已接近了尾声。今天设计了自己的Thread类库,支持以任意的java类函数或类实例函数为执行代码段创建子线程。

一、Test8.java(提供子线程的执行代码段)

import  com.rjos.driver.Text;
public class Test8
{
    /*
    以类函数为子线程的执行代码段
    */
    static public void TestClassThread1()
    {
        while(true)
        {
            Text.printfln("child thead:TestClassThread1");
        }

    }

    /*
    以类实例对象函数为子线程的执行代码段
    */
    public void TestInstanceThread1()
    {
        while(true)
        {
            Text.printfln("child thead:TestInstanceThread1");
        }
    }

    /*
    以类实例对象函数为子线程的执行代码段
    */
    public void TestInstanceThread2()
    {
        while(true)
        {
            Text.printfln("child thead:TestInstanceThread2");
        }
    }
}

  

二、Test7.java(创建子线程。本例子中分别通过类实例对象函数作为代码段创建了2个子线程但没有让子线程进入运行状态;通过类函数作为代码段创建了1条子线程,并且使它进入运行状态)

import com.rjos.Thread;

public class Test7
{
    static public Test2 a;
    public static void main(String[] args)
    {
        Test8 b =new Test8();
        int thread1=Thread.CreateThreadByInstanceMethod(b,"TestInstanceThread1");

        int thread2=Thread.CreateThreadByInstanceMethod(b,"TestInstanceThread2");

        int thread3=Thread.CreateThreadByClassMethod("Test8","TestClassThread1");

        //Thread.setThreadRunable(thread1);

        //Thread.setThreadRunable(thread2);

        Thread.setThreadRunable(thread3);

        while(true)
        {
            a= new Test2();
        }
    }
}

  

三、运行结果(见附件截图)

猜你喜欢

转载自rjpos.iteye.com/blog/2236946