threadstart – WordPress

您所在的位置:网站首页 调用start方法和run方法的区别 threadstart – WordPress

threadstart – WordPress

#threadstart – WordPress| 来源: 网络整理| 查看: 265

java中thread的start和run的区别

java中thread的start()方法和run()方法的区别:t.start(); 该行代码相当于是启动线程,t.run(); 该行代码相当于是使用t这个类中的run方法而已。

关于Thread类的start方法

分类: 电脑/网络 》》 程序设计 》》 其他编程语言 问题描述:

《Java就业培训教程》P180源码

程序清单:ThreadDemo3.java

public class ThreadDemo3

{public static void main(String args)

{

new TestThread ().start();

TestThread tt= new TestThread();创建TestThread类的一个实例

Thread t= new Thread(tt);创建一个Thread类的实例

t.start();使线程进入Runnable状态################

while(true)

{

System.out.println(“main thread is running“);

}}}

class TestThread implements Runnable extends Thread

{

public void run()线程的代码段,当执行start()时,线程从此出开始执行

{

while(true)

{

System.out.println(Thread.currentThread().getName() +

“ is running“);

}}}

问题:问题在有#############标志的行里。张孝祥的就业培训教程中说t.start()会调用实现了runnable的TestThread类中的run ()方法开始一个线程,但是t本身就是Thread的一个对象,也自然拥有Thread的run()方法才对,那样的话,怎么能够保证这里的t.start()会调用runnable的run 而不是Thread的run 呢?

估计查jdk的说明文档也可以找到,不过好费劲啊还或许看不明白。这里直接一些请教高人,本人考试在即,希望能说的尽量明白让初学者能够看懂请各位高人指点迷津,不吝赐教!

解析:

看一下Thread类的源码,也许你就能搞清楚为身么文档里这样解释。

这是Thread类的run()方法的代码。一目了然如果target存在的话执行target的run()方法,否则什么也不做。这样我们可以推测(因为Thread 实际运行的方法start0 是native方法 我们看不到它的实现)也就是说Thread的run()方法总是先被调用的,然后调用taget(构造函数中的Runnable对象)的run()方法。

public void run() {

if (target != null) {

target.run();

}

}

如果对于target的来历还有疑问,可以看一下构造函数的实现:

public Thread(Runnable target) {

init(null, target, “Thread-“ + nextThreadNum(), 0);

}

还没完那。。。, init 的实现:

private void init(ThreadGroup g, Runnable target, String name,

long stackSize) {

太多了,不贴了,总之target 就是那个Runnable了

}

说的不清楚么?总之Thread的start方法会调用自己的run方法,run方法会调用用于构造的Runnable对象的run方法。另一个编写自己的线程的方法就是extends Thread类并改写run方法。

threadstart()

用 ParameterizedThreadStart 啊。比如:public static void Main() { // To start a thread using a shared thread procedure, use // the class name and method name when you create the // ParameterizedThreadStart delegate. C# infers the // appropriate delegate creation syntax: // new ParameterizedThreadStart(Work.DoWork) // Thread newThread = new Thread(Work.DoWork);

// Use the overload of the Start method that has a // parameter of type Object. You can create an object that // contains several pieces of data, or you can pass any // reference type or value type. The following code passes // the integer value 42. // newThread.Start(42); // To start a thread using an instance method for the thread // procedure, use the instance variable and method name when // you create the ParameterizedThreadStart delegate. C# infers // the appropriate delegate creation syntax: // new ParameterizedThreadStart(w.DoMoreWork) // Work w = new Work(); newThread = new Thread(w.DoMoreWork);

// Pass an object containing data for the thread. // newThread.Start(“The answer.“); }

public static void DoWork(object data) { Console.WriteLine(“Static thread procedure.Data=’{0}’“, data); } public void DoMoreWork(object data) { Console.WriteLine(“Instance thread procedure.Data=’{0}’“, data); }

Python中Thread类的start和run方法的区别

若调用start,则先执行主线程的,后执行子线程的;若调用run,相当于函数调用,按照程序的顺序执行;1、start()方法来启动线程,真正实现了多线程运行。这时无需等待run方法体代码执行完毕,可以直接继续执行下面的代码;通过调用Thread类的start()方法来启动一个线程, 这时此线程是处于就绪状态, 并没有运行。 然后通过此Thread类调用方法run()来完成其运行操作的, 这里方法run()称为线程体,它包含了要执行的这个线程的内容, Run方法运行结束, 此线程终止。然后CPU再调度其它线程。2、run()方法当作普通方法的方式调用。程序还是要顺序执行,要等待run方法体执行完毕后,才可继续执行下面的代码; 程序中只有主线程——这一个线程, 其程序执行路径还是只有一条, 这样就没有达到写线程的目的。

java中thread的start()和run()有何区别

1、start()方法来启动线程,真正实现了多线程运行,这时无需等待。

run方法体代码执行完毕而直接继续执行下面的代码;通过调用Thread类的start()方法来启动一个线程,这时此线程是处于就绪状态,并没有运行。

通过Thread类调用方法run()来完成其运行操作的,这里方法run()称为线程体,它包含了要执行的这个线程的内容,Run方法运行结束,此线程终止,而CPU再运行其它线程。

2、run()方法当作普通方法的方式调用,程序还是要顺序执行,还是要等待run方法体执行完毕后才可继续执行下面的代码; 

而如果直接用run方法,这只是调用一个方法而已,程序中依然只有主线程–这一个线程,其程序执行路径还是只有一条,这样就没有达到写线程的目的。

3、调用start方法方可启动线程,而run方法只是thread的一个普通方法调用,还是在主线程里执行。

4、这两个方法需要把并行处理的代码放在run()方法中,start()方法启动线程将自动调用 run()方法,这是由jvm的内存机制规定的。并且run()方法必须是public访问权限,返回值类型为void.。

扩展资料:

用start方法来启动线程,真正实现了多线程运行,这时无需等待run方法体代码执行完毕而直接继续执行下面的代码。

通过调用Thread类的start()方法来启动一个线程,这时此线程处于就绪(可运行)状态,并没有运行,一旦得到cpu时间片,就开始执行run()方法,这里方法 run()称为线程体,它包含了要执行的这个线程的内容,Run方法运行结束,此线程随即终止。

run()方法只是类的一个普通方法而已,如果直接调用Run方法,程序中依然只有主线程这一个线程,其程序执行路径还是只有一条,还是要顺序执行,还是要等待run方法体执行完毕后才可继续执行下面的代码,这样就没有达到写线程的目的。

java thread start 方法会立即执行吗

楼主:在Java中是这样的,当你创建了一个线程,并调用了start方法,则该线程会开始执行,即立即执行。API说明如下:public void start()使该线程开始执行;Java 虚拟机调用该线程的 run 方法。结果是两个线程并发地运行;当前线程(从调用返回给 start 方法)和另一个线程(执行其 run 方法)。多次启动一个线程是非法的。特别是当线程已经结束执行后,不能再重新启动。有问题欢迎提问,满意请采纳,谢谢!

JAVA里thread.start与thread.join 哪个有前哪个在后

线程必须要先start,才能join,只有启动了,才能对线程进行操作。如有一个线程叫A,那么请看以下示例代码A.start(); //启动A线程A.join(); //邀请A线程先执行,本线程先暂停执行,等待A线程执行完后,主线程再接着往下执行System.out.println(“OK“); //这句话,要等到A线程执行完后,主线程获取到执行权后,才会被执行

这里的主线程是指执行以上三行代码的线程

多线程Thread 执行的方法(有参数与无参数)

我们看到Thread 的源文件可以看出是常用的执行方法有ParameterizedThreadStart、与ThreadStart

so,我们只需要了解ParameterizedThreadStart与ThreadStart区别就可以掌握了

下面看例子:

1、ThreadStart 这个委托定义为 无参 void ThreadStart()

ThreadStart threadStart=new ThreadStart(Calculate);

Thread thread=new Thread(threadStart);

thread.Start();

public void Calculate()

  {

 double Diameter=0.5;

 Console.Write(“The Area Of Circle with a Diameter of {0} is {1}“Diameter,Diameter*Math.PI);

  }

2、ParameterThreadStart的定义为void ParameterizedThreadStart(object state),使用这个这个委托定义的线程的启动函数可以接受一个输入参数,具体例子如下 :public static bool ControlSpeekingHornNew(string TowerCode, string even_type, string ip, int port, int isOpen, string buf)

{

             Thread_HornModel model = new Thread_HornModel();            model.TowerCode = TowerCode;

            model.ip = ip;

            model.port = port;

            model.even_type = even_type;

            model.isOpen = isOpen;

            model.buf = buf;

            Thread _thread = new Thread(new ParameterizedThreadStart(ThreadControlSpeekingHornNew));

            _thread.Start(model);

            return true;

}// 注意这里只能使用object类型,不能使用具体的 Thread_HornModel类型,传参进去再进行转换

public static void ThreadControlSpeekingHornNew(object resons)        {

            if(resons is Thread_HornModel)

            {

                Thread_HornModel _data = (Thread_HornModel)(resons);

                string TowerCode = _data.TowerCode;

                string even_type = _data.even_type;

                string ip = _data.ip;

                int port = _data.port;

                int isOpen = _data.isOpen;

                string buf = _data.buf

              // do something

            }

        }public class Thread_HornModel

    {

        public string TowerCode { get; set; }

        public string even_type { get; set; }

        public string ip { get; set; }

        public int port { get; set; }

        public int isOpen { get; set; }

        public string buf { get; set; }

    }

多线程问题:为什么无法调用Thread类的start方法

我来帮你解决把。。虽然我不明白你为什么说无法调用Thread类的start方法。。首先,你要理解多线程实现的2种方法。1.继承Thread类并覆写run方法2.实现runnable接口并覆写run方法。其中这两种方法中在实际开发和应用中只有第二种是规范和常见的(原因是Thread类也是实现了runnable接口的类,用接口可以实现更大的灵活性和避免单继承的局限性以及共享(这个在多线程中非常重要!!))下面来实现Thread类的start方法,先编写一个实现runnable接口的类publicclassRunnalbeDemoimplementsrunnable{publicvoidrun(){System.out.println(“helloworld!“);}}然后再编写另外一个类来调用Thread类的start方法publicclassThread{RunnalbeDemord=newRunnalbeDemo();newThread(rd,“线程A“).start();//用匿名对象来启动线程,其中Thread类的构造方法可以查阅jdk文档newThread(rd,“线程B“).start();newThread(rd,“线程C“).start();}这样就实现了3个线程的调用start方法。如果还不明白可以问我,或者在eclipse里面实验一下

C# 多线程,ThreadStart()里面的方法带了参数就提示错误

线程操作主要用到Thread类,他是定义在System.Threading.dll下。使用时需要添加这一个引用。该类提供给我们四个重载的构造函数(以下引自msdn)。 Thread (ParameterizedThreadStart) 初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托。 Thread (ThreadStart) 初始化 Thread 类的新实例。 由 .NET Compact Framework 支持。

Thread (ParameterizedThreadStart, Int32) 初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托,并指定线程的最大堆栈大小。 Thread (ThreadStart, Int32) 初始化 Thread 类的新实例,指定线程的最大堆栈大小。 由 .NET Compact Framework 支持。我们如果定义不带参数的线程,可以用ThreadStart,带一个参数的用ParameterizedThreadStart。带多个参数的用另外的方法,下面逐一讲述。一、不带参数的using System;using System.Collections.Generic;using System.Text;using System.Threading;namespace AAAAAA{ class AAA { public static void Main() { Thread t = new Thread(new ThreadStart(A)); t.Start(); Console.Read(); } private static void A() { Console.WriteLine(“Method A!“); } }}结果显示Method A!二、带一个参数的 由于ParameterizedThreadStart要求参数类型必须为object,所以定义的方法B形参类型必须为object。using System;using System.Collections.Generic;using System.Text;using System.Threading;namespace AAAAAA{ class AAA { public static void Main() { Thread t = new Thread(new ParameterizedThreadStart(B)); t.Start(“B“); Console.Read(); } private static void B(object obj) { Console.WriteLine(“Method {0}!“,obj.ToString ()); } }}结果显示Method B! 三、带多个参数的 由于Thread默认只提供了这两种构造函数,如果需要传递多个参数,我们可以自己将参数作为类的属性。定义类的对象时候实例化这个属性,然后进行操作。using System;using System.Collections.Generic;using System.Text;using System.Threading;namespace AAAAAA{ class AAA { public static void Main() { My m = new My(); m.x = 2; m.y = 3; Thread t = new Thread(new ThreadStart(m.C)); t.Start(); Console.Read(); } } class My { public int x, y; public void C() { Console.WriteLine(“x={0},y={1}“, this.x, this.y); } }}结果显示x=2,y=3 四、利用结构体给参数传值。定义公用的public struct,里面可以定义自己需要的参数,然后在需要添加线程的时候,可以定义结构体的实例。//结构体 struct RowCol { public int row; public int col; };//定义方法 public void Output(Object rc) { RowCol rowCol = (RowCol)rc; for (int i = 0; i 《 rowCol.row; i++) { for (int j = 0; j 《 rowCol.col; j++) Console.Write(“{0} “, _char); Console.Write(“\n“); } }



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3