java创建线程两种方式:

1.继承Thread创建线程

/*** Created by lsf on 16/4/18.*/
class NewThread extends Thread {NewThread(){super();    //创建线程start();    //启动线程
    }public void run() {long starttime = System.currentTimeMillis();System.out.println("child thread..."+starttime);}
}class CurrentThreadDemo {public static void main(String args[]) {long starttime2 = System.currentTimeMillis();System.out.println("main thread,,,"+starttime2);    //主线程new NewThread();System.out.println("住县城");}
}

2.实现

import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIConversion;import java.util.Date;/*** Created by lsf on 16/4/18.*/
class NewThread extends Thread {NewThread(){Thread t = new Thread(this);    //创建线程t.start();                      //启动线程
    }public void run() {long starttime = System.currentTimeMillis();System.out.println("child thread..."+starttime);}
}class CurrentThreadDemo {public static void main(String args[]) {long starttime2 = System.currentTimeMillis();System.out.println("main thread,,,"+starttime2);new NewThread();System.out.println("主线程");  //主线程
    }
}

3.给任务创建多个线程去执行

class NewThread extends Thread {String name;NewThread(String threadname){name = threadname;Thread t = new Thread(this,threadname);    //创建线程t.start();                      //启动线程
    }public void run() {long starttime = System.currentTimeMillis();System.out.println("child thread..."+name);}
}class CurrentThreadDemo {public static void main(String args[]) {long starttime2 = System.currentTimeMillis();System.out.println("main thread,,,"+starttime2);new NewThread("demo1");new NewThread("demo2");new NewThread("demo3");System.out.println("主线程");  //主线程
    }
}

4.线程优先级设置

/*** Created by lsf on 16/4/22.*/class ThreadTest implements Runnable {Thread t;int count = 0;private volatile Boolean flag = true;public ThreadTest(int p) {t = new Thread(this);t.setPriority(p);}public void start(){t.start();}public void finish(){flag = false;}@Overridepublic void run() {while(flag){count++;}}}public class ThreadPriority {public static void main(String[] args) {ThreadTest t1 = new ThreadTest(Thread.NORM_PRIORITY - 2);ThreadTest t2 = new ThreadTest(Thread.NORM_PRIORITY + 2);t1.start();t2.start();t1.finish();t2.finish();try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}try {System.out.println("t1 count:"+t1.count);System.out.println("t2 count:"+t2.count);t1.t.join();t2.t.join();System.out.println("t1 is alive:" + t1.t.isAlive());System.out.println("t2 is alive:" + t1.t.isAlive());} catch (InterruptedException e) {e.printStackTrace();}}
}

5.线程同步

线程同步的关键在于同一时刻线程在管程内,应用场景一般是:当某个方法(callme)需要用多线程去执行,可以改造一下对应的方法,加上关键词synchronized,这样在调用过程中,每个线程都会默认进入隐式管程。

/*** Created by root on 16-4-15.*/class Callme {synchronized void call(String msg){System.out.println("["+msg);try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("]");}
}class ThreadCaller implements Runnable{String msg;Callme target;Thread t;public ThreadCaller(Callme targ,String s) {target = targ;msg = s;t = new Thread(this);t.start();}@Overridepublic void run() {target.call(msg);}
}class Demo {public static void main(String[] args) {Callme target = new Callme();ThreadCaller obj1 = new ThreadCaller(target,"Hello");ThreadCaller obj2 = new ThreadCaller(target,"Synchronized");ThreadCaller obj3 = new ThreadCaller(target,"World");try {obj1.t.join();obj2.t.join();obj3.t.join();} catch (InterruptedException e) {System.out.println("Interrupted");}}
}

  

转载于:https://www.cnblogs.com/alexkn/p/5418084.html

java线程跟多线程相关推荐

  1. java线程池多线程优先级_Java线程优先级

    java线程池多线程优先级 Priority of a thread describes how early it gets execution and selected by the thread ...

  2. java线程wait_java多线程学习(四) 之 wait和notify

    ynchronized让线程之间互斥,但是有时候因为业务逻辑,需要主动释放锁,让其它的线程执行,这就需要线程之间通信,我们知道JAVA里每个对象都有个隐式锁,JAVA为每个对象都提供了wait和not ...

  3. java线程 demo_Java多线程demo

    近期考虑采用Java多线程实现给用户发短信的功能.自己做了一个简单的demo. demo需求如下:通过界面输入用户名.密码.手机号,点击添加,即可实时的为该用户发一条短信. 实现过程如下: 2.点击& ...

  4. Java线程之多线程与多进程(3)——Java中的多线程

    单线程 任何程序至少有一个线程,即使你没有主动地创建线程,程序从一开始执行就有一个默认的线程,被称为主线程,只有一个线程的程序称为单线程程序.如下面这一简单的代码,没有显示地创建一个线程,程序从mai ...

  5. 什么是java线程_Java多线程是什么意思?

    展开全部 Java多线程实现方式主要有三种:继承Thread类.实现Runnable接口.使用ExecutorService.Callable.Future实现有返回结果的多636f707932313 ...

  6. 【Java线程】多线程实现简单的一对一聊天

    实现原理: 1.主方法:在主方法中构建一个输入流接收键盘的输入,根据接收到的输入信息判定角色是服务器端还是客户端:然后构建相应的socket,开启线程: 2.线程-坚守键盘阵地:接收键盘的输入,将从键 ...

  7. Java线程之多线程与多进程(1)——以操作系统的角度述说线程与进程

    任务调度 线程是什么?要理解这个概念,须要先了解一下操作系统的一些相关概念.大部分操作系统(如Windows.Linux)的任务调度是采用时间片轮转的抢占式调度方式,也就是说一个任务执行一小段时间后强 ...

  8. java 线程(多线程)thread,案例:汉字打字练习(AWT-EventQuecue,AWT-Windows)

    实验目的 当Java程序包含图形用户界面(GUI)时,Java虚拟机在运行应用程序时会自动启动更多的线程,其中有两个重要的线程:AWT-EventQuecue和 AWT-Windows. AWT-Ev ...

  9. Java线程之多线程与多进程(2)——线程优先级与线程安全

    线程优先级 现在主流操作系统(如Windows.Linux.Mac OS X)的任务调度除了具有前面提到的时间片轮转的特点外,还有优先级调度(Priority Schedule)的特点.优先级调度决定 ...

最新文章

  1. 13 登陆_13级!凌晨,“黑格比”登陆!对莆田的最新影响……
  2. HDU - 6186 CS Course(维护前缀+后缀)
  3. SonarQube6.2源码解析(四)
  4. Sping : @InitBinder注解
  5. 【BZOJ】3495: PA2010 Riddle 2-SAT算法
  6. css中伪元素也可以和css类相结合
  7. linux下如何添加定时备份任务,Linux下Oracle设置定时任务备份数据库的教程
  8. YACC(BISON)使用指南
  9. sqlyog的快捷键
  10. (哈工大)计算机网络体系结构——OSI、TCP/IP、5层模型
  11. 安卓手机网易云视频,下载的文件位置:
  12. [机器学习] - 岭回归与Lasso回归
  13. VirtualBox靶机启动失败:end Kernel panic - not syncing: Attempted to kill the idle task
  14. SAP FICO 财务月结--自动清账
  15. rdkitpython | 挑选多个互变异构体的主要构象
  16. 创业篇之一、赢家不会告诉你的事情
  17. Assignment of attribute weights with belief distributions for MADM under uncertainties
  18. The JTAG/SMC Hack On Xbox360
  19. 音乐疗法可缓解精神分裂症状
  20. html 上下左右键,怎么样使用键盘的上下左右键

热门文章

  1. UVA 694-The Collatz Sequence
  2. 机房收费管理系统 之 总结
  3. 深入探究ConvNets vs. Transformers,哪种预训练模型的可迁移性更好?
  4. 推荐几篇开源论文,包含人脸、目标检测跟踪、分割、去噪、超分辨率等
  5. 必看总结!深度学习时代您应该阅读的10篇文章了解图像分类!
  6. 【机器学习】机器学习从零到掌握之十二 -- 教你使用决策树预测隐形眼镜类型
  7. ACM Fellow发文抨击【同行评审】作弊轻松中顶会
  8. YOLO-Fastest算法!准确率接近YOLOv3,速度快上45%
  9. Github | 如何用Python测试GPU与CPU计算性能
  10. C# + OpenCvSharp实现仿射变换