多线程(实际开发)

实际开发中当某些代码需要同时执行时就使用多线程进行单独封装。

使用多线程封装的方法有:匿名内部类等

演示:

class ThreadDemo
{
    public static void main(String[] args) 
    {
        //使用匿名内部类的方法将要运行的代码放入线程中
        new Thread()
        {
            public void run()
            {
                for( int i=0; i<60; i++)
                {
                    System.out.println(Thread.currentThread().getName()+"..Thread..."+i);
                }    
            }
        }.start();
        //实际开发中实现接口的写法
        Runnable r = new Runnable()
        {
            public void run()
            {
                for( int i=0; i<60; i++)
                {
                    System.out.println(Thread.currentThread().getName()+"...Runnable..."+i);
                }
            }
        }
        new Thread(r).start();

    }
}
实际开发中使用线程的几种方法
原文地址:https://www.cnblogs.com/gzc911/p/4909022.html