java多线程(1) 线程的基本概念
一、线程的基本概念:


package com.cy.thread;
public class TestThread1 {
public static void main(String[] args) {
Runner1 r = new Runner1();
Thread t = new Thread(r);
//启动一个线程,线程启动必须调用Thread类的start()方法
//start方法会通知cpu,我现在有个新线程了啊,已经准备好了,您老人家什么时候有时间赶紧给我点时间片。
t.start();
for(int i=0; i<100; i++){
System.out.println("Main Thread:----- " + i);
}
}
}
/**
* 实现了Runnable之后,jdk就知道了,这是一个线程类。
*/
class Runner1 implements Runnable{
@Override
public void run() {
for(int i=0; i<100; i++){
System.out.println("Runner1: " + i);
}
}
}
console:

package com.cy.thread;
public class TestThread1 {
public static void main(String[] args) {
Runner1 r = new Runner1();
r.start();
for(int i=0; i<100; i++){
System.out.println("Main Thread:----- " + i);
}
}
}
class Runner1 extends Thread{
@Override
public void run() {
for(int i=0; i<100; i++){
System.out.println("Runner1: " + i);
}
}
}

isAlive(): 判断线程是否还活着,即线程是否还未终止;就绪、运行、阻塞叫活着。终止了就死了。线程创建完还没启动那也是死的。
getPriority() 获得线程的优先级数值;优先级越高的线程获得的cpu执行的时间越多。
setPriority() 设置线程的优先级数值;
Thread.sleep() 将当前线程睡眠指定毫秒数。
join() 合并某个线程。
yield() 高风亮节,让出cpu,让其他的线程执行,而自己进入就绪状态。
wait() 当前线程进入对象的wait pool;
notify、notifyAll() 唤醒对象的wait pool中的一个/所有等待线程。
五、sleep方法:
package com.cy.thread;
import java.util.Date;
public class TestInterrupt {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
try {
/**
* 在哪个线程里面调用sleep方法,就让哪个线程睡眠。
*/
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
/**
* interrupt()方法:thread线程在睡眠的时候,将它打断。
* 这里为了例子演示,让子线程结束。
* 但这不是让子线程结束的最好方法。
*/
thread.interrupt();
}
}
class MyThread extends Thread{
@Override
public void run() {
while(true){
System.out.println("==="+new Date()+"===");
try {
sleep(1000);
} catch (InterruptedException e) {
return;
}
}
}
}
console打印:

/**
* 提供一个让线程结束的方法:
* thread.flag = false; run()方法就不再执行了,run方法一结束,线程就结束了。
*/
class MyThread2 extends Thread{ boolean flag = true; @Override
public void run() {
while(flag){
System.out.println("==="+new Date()+"===");
try {
sleep(1000);
} catch (InterruptedException e) {
return;
}
} }
}
六、join方法:
package com.cy.thread;
public class TestJoin {
public static void main(String[] args) {
MyThread2 t1 = new MyThread2("t1");
t1.start();
try {
/**
* 将t1线程合并到main线程,和main线程一块执行。
*/
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
for(int i=0; i<=5; i++){
System.out.println("i am main thread");
}
}
}
class MyThread2 extends Thread{
MyThread2(String s){
super(s);
}
@Override
public void run() {
for(int i=0; i<=5; i++){
System.out.println("i am " + getName());
try{
sleep(1000);
}catch(InterruptedException e){
return;
}
}
}
}

package com.cy.thread;
public class TestYield {
public static void main(String[] args) {
//可以使用同一个线程类,new两个线程。
MyThread3 t1 = new MyThread3("t1");
MyThread3 t2 = new MyThread3("t2");
t1.start();
t2.start();
}
}
class MyThread3 extends Thread{
MyThread3(String s){
super(s);
}
@Override
public void run() {
for(int i=0; i<=100;i++){
System.out.println(getName() + ": " +i);
if(i%10 == 0){
yield();
}
}
}
}
Java提供一个线程调度器来监控程序中启动后进入就绪状态的所有线程。
线程调度器按照线程的优先级决定应调度哪个线程来执行。
线程的优先级用数字表示,范围从1-10,一个线程默认的优先级是5;
Thread.MIN_PRIORITY = 1;
Thread.MAX_PRIORITY = 10;
Thread.NORM_PRIORITY = 5;
使用下述方法获得或设置线程对象的优先级:
int getPriority();
void setPriority(int newPriority);
package com.cy.thread;
public class TestPriority {
public static void main(String[] args) {
Thread t1 = new Thread(new T1());
Thread t2 = new Thread(new T2());
t1.setPriority(Thread.NORM_PRIORITY + 3);
t1.start();
t2.start();
}
}
class T1 implements Runnable{
@Override
public void run() {
for(int i=0; i<1000; i++){
System.out.println("T1: " + i);
}
}
}
class T2 implements Runnable{
@Override
public void run() {
for(int i=0; i<1000; i++){
System.out.println("----T2: " + i);
}
}
}
java多线程(1) 线程的基本概念的更多相关文章
- java多线程_01_线程的基本概念
线程:一个程序里边不同的执行路径 例子程序:这个例子程序是一条执行路径.这个程序只有一个分支,就是main方法,叫主线程 public static void main(String[] args) ...
- Java多线程之线程的通信
Java多线程之线程的通信 在总结多线程通信前先介绍一个概念:锁池.线程因为未拿到锁标记而发生的阻塞不同于前面五个基本状态中的阻塞,称为锁池.每个对象都有自己的锁池的空间,用于放置等待运行的线程.这些 ...
- Java多线程父子线程关系 多线程中篇(六)
有的时候对于Java多线程,我们会听到“父线程.子线程”的概念. 严格的说,Java中不存在实质上的父子关系 没有方法可以获取一个线程的父线程,也没有方法可以获取一个线程所有的子线程 子线程的消亡与父 ...
- Java多线程02(线程安全、线程同步、等待唤醒机制)
Java多线程2(线程安全.线程同步.等待唤醒机制.单例设计模式) 1.线程安全 如果有多个线程在同时运行,而这些线程可能会同时运行这段代码.程序每次运行结果和单线程运行的结果是一样的,而且其他的变量 ...
- java多线程与线程间通信
转自(http://blog.csdn.net/jerrying0203/article/details/45563947) 本文学习并总结java多线程与线程间通信的原理和方法,内容涉及java线程 ...
- java多线程之线程的同步与锁定(转)
一.同步问题提出 线程的同步是为了防止多个线程访问一个数据对象时,对数据造成的破坏. 例如:两个线程ThreadA.ThreadB都操作同一个对象Foo对象,并修改Foo对象上的数据. publicc ...
- Java多线程与线程同步
六.多线程,线程,同步 ①概念: 并行:指两个或多个在时间同一时刻发生(同时发生) 并发:指两个或多个事件在同一时间段内发生 具体概念: 在操作系统中,安装了多个程序,并发指的是在一段时间内宏观上有多 ...
- Java多线程之线程协作
Java多线程之线程协作 一.前言 上一节提到,如果有一个线程正在运行synchronized 方法,那么其他线程就无法再运行这个方法了.这就是简单的互斥处理. 假如我们现在想执行更加精确的控制,而不 ...
- Java多线程| 01 | 线程概述
Java多线程| 01 | 线程概述 线程相关概念 进程与线程 进程:进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是操作系统进行资源分配与调度的基本单位.可以把进程简单的理解 ...
- Java多线程之线程其他类
Java多线程之线程其他类 实际编码中除了前面讲到的常用的类之外,还有几个其他类也有可能用得到,这里来统一整理一下: 1,Callable接口和Future接口 JDK1.5以后提供了上面这2个接口, ...
随机推荐
- Windows 下 左Ctrl和Caps交换
将以下文本存为后缀为.reg文件,运行并重启即可 左Ctrl和Caps交换 Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SYSTE ...
- 在QT中使用静态对象
最近做教研室的项目,需要只能存在一个接收数据的线程,那么我就想把这个线程设置成一个静态对象.但是在connect信号与槽的时候出了一点问题,最后搞好了,现在这mark一下: 比如说一个声明了一个静态的 ...
- LeetCode OJ : Different Ways to Add Parentheses(在不同位置增加括号的方法)
Given a string of numbers and operators, return all possible results from computing all the differen ...
- 《Effective C++》第3章 资源管理(2)-读书笔记
章节回顾: <Effective C++>第1章 让自己习惯C++-读书笔记 <Effective C++>第2章 构造/析构/赋值运算(1)-读书笔记 <Effecti ...
- C++面向对象高级编程(五)类与类之间的关系
技术在于交流.沟通,转载请注明出处并保持作品的完整性. 本节主要介绍一下类与类之间的关系,也就是面向对象编程先介绍两个术语 Object Oriented Programming OOP面向对象编 ...
- APUE学习笔记——5缓冲Buffering、流、文件对象
缓冲的几个基本概念 缓冲的作用:减少系统read和write的次数. 全缓冲 系统标准I/O缓冲区被写满时才进行真正的I/O操作. 磁盘文件一般使用全缓冲 ...
- 修改myelipse中部署路径deploy location内容的方法
在new web project 中的project name等内容,可以打开.mymetadata文件进行修改 <?xml version="1.0" encoding=& ...
- 源码编译tmux
(1)clone 源代码仓库: $ git clone https://github.com/tmux/tmux.git (2) 编译之前先安装libevent,去官网下载tar包: http://l ...
- Git远程操作详解(转)
转自:http://www.ruanyifeng.com/blog/2014/06/git_remote.html Git远程操作详解 Git是目前最流行的版本管理系统,学会Git几乎成了开发者的 ...
- HDU 2268
http://acm.hdu.edu.cn/showproblem.php?pid=2268 小学四年级应用题,让我找回了儿时的快乐... #include <iostream> #inc ...