标签:
继承class Thread方法(java.lang) | 实现interface Runnable接口(java.lang) |
构造方法,run()方法 | run()方法 |
start()方法,启动线程 sleep()方法,线程休眠 join()方法,使其他线程等待当前线程终止 yield()方法,当前运行线程释放处理器资源 |
|
获取线程引用,static Thread currentThread(),返回当前运行线程的引用 |
package com.buaa.test;
public class MyThread extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
System.out.println("MyThrerad中的run方法");
}
}
}
package com.buaa.test
public class Test {
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start();
while(true){
System.out.println("main方法中的线程在运行");
}
}
}
package com.buaa.test2;
public class PrivateThread implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
System.out.println("这里是私人的线程啊......");
}
}
}
package com.buaa.test2;
public class Test2 {
public static void main(String[] args) {
PrivateThread pThread = new PrivateThread();
Thread thread = new Thread(pThread);
thread.start();
while(true){
System.out.println("main方法中的线程啊.....");
}
}
}
package com.buaa.test3;
public class DaemonThread implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
System.out.println(Thread.currentThread().getName()+"------is running.");
}
}
}
package com.buaa.test3;
public class Test3 {
public static void main(String[] args) {
DaemonThread dThread = new DaemonThread();
Thread thread =new Thread(dThread,"后台线程");
thread.setDaemon(true); //将thread线程设置成为后台线程
thread.start();
}
}
package com.buaa.test4;
public class MinPriority implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0 ;i < 10 ;i++){
System.out.println(Thread.currentThread().getName()+"正在输出:"+i);
}
}
}
package com.buaa.test4;
public class MaxPrority implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0 ;i < 10 ;i++){
System.out.println(Thread.currentThread().getName()+"正在输出:"+i);
}
}
}
package com.buaa.test4;
public class Test4 {
public static void main(String[] args) {
Thread maxThread = new Thread(new MaxPriority(),"高优先级");
Thread minThread = new Thread(new MinPriority(),"低优先级");
maxThread.setPriority(10);
minThread.setPriority(Thread.MIN_PRIORITY);
maxThread.start();
minThread.start();
}
}
package com.buaa.test5;
public class SleepThread implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0 ; i < 10;i++){
if(i==3){
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("线程正在输出:"+i);
}
}
}
package com.buaa.test5;
public class Test5 {
public static void main(String[] args) {
Thread thread = new Thread(new SleepThread());
thread.start();
}
}
package com.buaa.test6;
public class YieldThread implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0 ;i <5 ;i++){
System.out.println(Thread.currentThread().getName()+"---------"+i);
if(i==3){
System.out.println("线程让步:");
Thread.yield();
}
}
}
}
package com.buaa.test6;
public class Test6 {
public static void main(String[] args) {
Thread t1 = new Thread(new YieldThread(),"线程A");
Thread t2 = new Thread(new YieldThread(),"线程B");
t1.start();
t2.start();
}
}
package com.buaa.test7;
public class EnergencyThread implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0 ; i < 6 ;i++){
System.out.println(Thread.currentThread().getName()+"输入:"+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package com.buaa.test7;
public class Test7 {
public static void main(String[] args) {
Thread t = new Thread(new EnergencyThread(),"插队线程");
t.start();
for(int i = 0 ;i<6 ;i++){
System.out.println(Thread.currentThread().getName()+"输入:"+i);
if(i==2){
try {
t.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
synchronized(lock){
操作资源
}
package com.buaa.test8;
public class Ticket1 implements Runnable{
private int ticket = 10;
Object lock = new Object();
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
synchronized (lock) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(ticket>0){
System.out.println(Thread.currentThread().getName()+"卖出的票"+ticket--);
}else{
break;
}
}
}
}
}
package com.buaa.test8;
public class Test8 {
public static void main(String[] args) {
Ticket1 ticket1 = new Ticket1();
new Thread(ticket1,"线程一").start();
new Thread(ticket1,"线程二").start();
new Thread(ticket1,"线程三").start();
new Thread(ticket1,"线程四").start();
}
}
synchronized 返回值类型 方法名(参数一,参数二){
}
package com.buaa.test9;
public class TicketMethod implements Runnable{
private int tickets = 10;
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
saleTicket();
if(tickets<=0){
break;
}
}
}
private synchronized void saleTicket() {
// TODO Auto-generated method stub
if(tickets>0){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"正在卖票"+tickets--);
}
}
}
package com.buaa.test9;
public class Test9 {
public static void main(String[] args) {
TicketMethod tMethod = new TicketMethod();
new Thread(tMethod,"线程一").start();
new Thread(tMethod,"线程二").start();
new Thread(tMethod,"线程三").start();
new Thread(tMethod,"线程四").start();
}
}
sleep() | yield() | wait() | |
来自于的类 | Thread.sleep(1000); | Thread.yield(); | obj.wait(); |
机理 | 使现在运行的线程暂停。在同步代码块中没有释放锁。是静态方法,只能控制当前正在运行的线程休眠,休眠结束后,会返回到就绪状态。 | 使现在运行的线程暂停。但是不会阻塞该线程,只是将该线程转换成就绪状态,让系统的调度重新调度一次。 | wait()方法释放了同步锁进入等待,直到其他线程进入同步锁,并调用notify()方法唤醒该线程为止 |
适用范围 | 任何地方 | 任何地方 | 同步代码块中,同notify()一起使用 |
异常情况 | 需要捕获异常 | 不需要 | 不需要 |
notify()方法,用于唤醒此同步锁上等待的第一个调用wait()方法的线程。 notifyAll(),唤醒所以等待的线程 |
标签:
原文地址:http://blog.csdn.net/shelter_lemon/article/details/51504571