实例方法有:
checkAccess()
判定当前运行的线程是否有权修改该线程。
getContextClassLoader()
返回该线程的上下文 ClassLoader。
getId()
返回该线程的标识符
getName()
返回该线程的名称。
getPriority()
返回线程的优先级。
isAlive()
测试线程是否处于活动状态。
start()
使该线程开始执行;Java 虚拟机调用该线程的 run 方法。
run()
如果该线程是使用独立的 Runnable 运行对象构造的,则调用该 Runnable 对象的 run 方法;否则,该方法不执行任何操作并返回。
。。。等等
类方法:
最常用的有
sleep(long millis)
在指定的毫秒数内让当前正在执行的线程休眠(暂停执行)。
sleep(long millis, int nanos)
在指定的毫秒数加指定的纳秒数内让当前正在执行的线程休眠(暂停执行)
currentThread()
返回对当前正在执行的线程对象的引用。
yield()
暂停当前正在执行的线程对象,并执行其他线程。
。。。等等
具体参考下面网址: java.io.Thread
一、继承Thread类创建线程子类
1.在这子类中重写run方法,在run方法内写线程任务代码
2.创建该子类实例,即是创建了一个线程实例
3.调用该实例的start方法来启动该线程
二、建一个类去实现Runnable接口
1.该类去实现接口的run方法,run方法内写线程任务代码
2.创建该类实例,把该实例当作一个标记target传给Thread类,如:Thread t = new Thread(该类实例);即创建一个线程对象
3.调用线程的star方法来启用该线程
public class TestMain {
public static void main(String[] args) {
//调用线程1
new ThreadTest1().start();
//调用线程2
ThreadTest2 t2 = new ThreadTest2();
new Thread(t2).start();
}
}
//实现多线程方式1,通过继承Thread类来实现
class ThreadTest1 extends Thread{
public void run(){
System.out.println("执行线程1。");
}
}
//实现多线程方式2,通过实现Runnable接口来实现
class ThreadTest2 implements Runnable{
public void run(){
System.out.println("执行线程2。");
}
}
以前在远标学过有三种:(1)继承Thread类,重写run函数
创建:
class xx extends Thread{
public void run(){
Thread.sleep(1000) //线程休眠1000毫秒,sleep使线程进入Block状态,并释放资源
}}
开启线程:
对象.start() //启动线程,run函数运行
(2)实现Runnable接口,重写run函数
开启线程:
Thread t = new Thread(对象) //创建线程对象
t.start()
(3)实现Callable接口,重写call函数
Callable是类似于Runnable的接口,实现Callable接口的类和实现Runnable的类都是可被其它线程执行的任务。
Callable和Runnable有几点不同:
①Callable规定的方法是call(),而Runnable规定的方法是run().
②Callable的任务执行后可返回值,而Runnable的任务是不能返回值的
③call()方法可抛出异常,而run()方法是不能抛出异常的。
④运行Callable任务可拿到一个Future对象,Future表示异步计算的结果。它提供了检查计算是否完成的方法,以等
待计算的完成,并检索计算的结果.通过Future对象可了解任务执行情况,可取消任务的执行,还可获取任务执行的结果
1、通过继承Thread类创建线程(1).首先定义一个类去继承Thread父类,重写父类中的run()方法。
在run()方法中加入具体的任务代码或处理逻辑。(2).直接创建一个ThreadTest类的对象,也可以利用多态性,变量声明为父类的类型。
(3).调用start方法,线程启动,隐含的调用run()方法。[java] view plain copypublic class ThreadTest extends Thread{ public void run(){ for(int i=0;i<=10;i++){ System.out.println(i); } } public static void main(String[] args) { ThreadTest thread1=new ThreadTest(); ThreadTest thread2=new ThreadTest(); thread1.start(); thread2.start(); } } 2、通过实现Runnable接口创建线程(1).定义一个类实现Runnable接口,重写接口中的run()方法。
在run()方法中加入具体的任务代码或处理逻辑。(2).创建Runnable接口实现类的对象。
(3).创建一个ThreadTest类的对象,需要封装前面Runnable接口实现类的对象。(接口可以实现多继承)(4).调用Thread对象的start()方法,启动线程 [java] view plain copypublic class ThreadTest implements Runnable{ @Override public void run() { for(int i=0;i<=10;i++){ System.out.println(i); } } public static void main(String[] args) { ThreadTest threadTest=new ThreadTest(); Thread theard=new Thread(threadTest); theard.start(); } } 3.通过Callable和Future创建线程 (1)创建Callable接口的实现类,并实现call()方法,该call()方法将作为线程执行体,并且有返回值。
(2)创建Callable实现类的实例,使用FutureTask类来包装Callable对象,该FutureTask对象封装了该Callable对象的call()方法的返回值。(3)使用FutureTask对象作为Thread对象的target创建并启动新线程。
(4)调用FutureTask对象的get()方法来获得子线程执行结束后的返回值 [java] view plain copypublic class ThreadTest implements Callable{ @Override public Integer call() throws Exception { int count =0; for(int i=0;i<=10;i++){ count=count+i; } return count; } public static void main(String[] args) throws InterruptedException, ExecutionException { ThreadTest test=new ThreadTest(); FutureTask thread = new FutureTask(test); new Thread(thread,"有返回值的线程").start(); System.out.println(thread.get()); } } 使用实现Runnable接口方式创建线程可以共享同一个目标对象(TreadDemo1 tt=new TreadDemo1();),实现了多个相同线程处理同一份资源。然后再看一段来自JDK的解释:The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments calledrun.This interface is designed to provide a common protocol for objects that wish to execute code while they are active. For example,Runnable is implemented by classThread. Being active simply means that a thread has been started and has not yet been stopped.In addition, Runnable provides the means for a class to be active while not subclassingThread. A class that implementsRunnable can run without subclassingThread by instantiating aThread instance and passing itself in as the target. In most cases, theRunnable interface should be used if you are only planning to override therun() method and no otherThread methods. This is important because classes should not be subclassed unless the programmer intends on modifying or enhancing the fundamental behavior of the class.采用实现Runnable、Callable接口的方式创见多线程时,优势是:线程类只是实现了Runnable接口或Callable接口,还可以继承其他类。
在这种方式下,多个线程可以共享同一个target对象,所以非常适合多个相同线程来处理同一份资源的情况,从而可以将CPU、代码和数据分开,形成清晰的模型,较好地体现了面向对象的思想。劣势是:编程稍微复杂,如果要访问当前线程,则必须使用Thread.currentThread()方法。
采用继承Thread类方式:(1)优点:编写简单,如果需要访问当前线程,无需使用Thread.currentThread()方法,直接使用this,即可获得当前线程。(2)缺点:因为线程类已经继承了Thread类,所以不能再继承其他的父类。
采用实现Runnable接口方式:(1)优点:线程类只是实现了Runable接口,还可以继承其他的类。在这种方式下,可以多个线程共享同一个目标对象,所以非常适合多个相同线程来处理同一份资源的情况,从而可以将CPU代码和数据分开,形成清晰的模型,较好地体现了面向对象的思想。
(2)缺点:编程稍微复杂,如果需要访问当前线程,必须使用Thread.currentThread()方法。
你new一个线程,右键下很多属性的,网上也很多比如
Priority 线程的优先级
Name线程的名字
ManagedThreadId 线程的唯一标识
Start启动线程
Resume 唤醒以挂起的线程
Suspend挂起线程
其中Sleep()是静态方法只能通过类调用。
线程的用处,最好的就是多线程啦,对于winform程序来说,多线程适解决winform程序ui界面体验的最佳选择了。线程可以提高性能,但是太多线程反而会影响性能,管理线程是比较麻烦的事
Java多线程的创建及启动
Java中线程的创建常见有如三种基本形式
1.继承Thread类,重写该类的run()方法。
复制代码
1 class MyThread extends Thread {
2
3 private int i = 0;
4
5 @Override
6 public void run() {
7 for (i = 0; i myCallable = new MyCallable(); // 创建MyCallable对象
6 FutureTaskft = new FutureTask(myCallable); //使用FutureTask来包装MyCallable对象
7
8 for (int i = 0; i {
32 private int i = 0;
33
34 // 与run()方法不同的是,call()方法具有返回值
35 @Override
36 public Integer call() {
37 int sum = 0;
38 for (; i implements RunnableFuture{
2
3 //。.
4
5 }
1 public interface RunnableFutureextends Runnable, Future{
2
3 void run();
4
5 }
于是,我们发现FutureTask类实际上是同时实现了Runnable和Future接口,由此才使得其具有Future和Runnable双重特性。通过Runnable特性,可以作为Thread对象的target,而Future特性,使得其可以取得新创建线程中的call()方法的返回值。
执行下此程序,我们发现sum = 4950永远都是最后输出的。而“主线程for循环执行完毕..”则很可能是在子线程循环中间输出。由CPU的线程调度机制,我们知道,“主线程for循环执行完毕..”的输出时机是没有任何问题的,那么为什么sum =4950会永远最后输出呢?
原因在于通过ft.get()方法获取子线程call()方法的返回值时,当子线程此方法还未执行完毕,ft.get()方法会一直阻塞,直到call()方法执行完毕才能取到返回值。
上述主要讲解了三种常见的线程创建方式,对于线程的启动而言,都是调用线程对象的start()方法,需要特别注意的是:不能对同一线程对象两次调用start()方法。
你好,本题已解答,如果满意
请点右下角“采纳答案”。
1、添加线程相关的头文件:#include<pthread.h>
2、线程创建函数是pthread_create()函数,该函数的原型为:
int pthread_create(pthread_t *thread,pthread_attr_t *attr,void* (*start_routine)(void*),void *arg);
3、线程退出函数是pthread_exit()函数,该函数的原型为:
void pthread_exit(void *retval);
创建线程的示例程序如下:
/***程序说明:创建线程函数pthread_create()函数的使用。*/#include <stdio.h>#include <pthread.h>#include <unistd.h>#include <stdlib.h>#include <string.h>; //打印标识符的函数void print_ids(const char *str){ pid_t pid; //进程标识符 pthread_t tid; //线程标识符 pid=getpid(); //获得进程号 tid=pthread_self(); //获得线程号 printf("%s pid:%u tid:%u (0x%x)\n", str,(unsigned int)pid,(unsigned int)tid,(unsigned int)tid); //打印进程号和线程号} //线程函数void* pthread_func(void *arg){ print_ids("new thread:"); //打印新建线程号 return ((void*)0);} //主函数int main(){ int err; pthread_t ntid; //线程号 err=pthread_create(&ntid,NULL,pthread_func,NULL); //创建一个线程 if(err != 0) { printf("create thread failed:%s\n",strerror(err)); exit(-1); } print_ids("main thread:"); //打印主线程号 sleep(2); return 0;}
java创建线程的方式有三种
第一种是继承Thread类 实现方法run() 不可以抛异常 无返回值
第二种是实现Runnable接口 实现方法run() 不可以抛异常 无返回值
第三种是实现Callable<T>;接口,接口中要覆盖的方法是 public <T> call() 注意:此方法可以抛异常,而前两种不能 而且此方法可以有返回值
第三种如何运行呢 Callable接口在util.concurrent包中,由线程池提交
import java.util.concurrent.*;
ExecutorService e = Executors.newFixedThreadPool(10); 参数表示最多可以运行几个线程
e.submit(); 这个里面参数传 实现Callable接口那个类的对象
声明:本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
蜀ICP备2020033479号-4 Copyright © 2016 学习鸟. 页面生成时间:2.617秒