package org.demo.thread;
public class Demo {
/**
* 简单的一个线程
* 启动一个线程
* 两种方式:implements Runnable,extends Thread
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyRunnable runnable=new MyRunnable();
Thread thread=new Thread(runnable);
thread.start();
for (int i = 0; i < 100; i++) {
System.out.println("MAIN:"+i);
}
}
}
class MyRunnable implements Runnable{
@Override
public void run() {
for (int i = 0; i <100; i++) {
System.out.println("RUNNABLE:"+i);
}
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
package org.demo.thread;
import java.util.Date;
public class Demo1 {
/**
* 线程睡眠,以及怎么跳出线程
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DemoSleep sleep=new DemoSleep();
Thread thread=new Thread(sleep);
thread.start();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
Thread.interrupted();
}
}
}
class DemoSleep implements Runnable{
@Override
public void run() {
while(true){
System.out.println("=========="+new Date());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
return;
}
}
}}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
package org.demo.thread;
public class Demo2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
T1 t1=new T1();
T2 t2=new T2();
t1.start();
//线程设置优先级,默认是5,最小是1,最大是10
t1.setPriority(Thread.NORM_PRIORITY+3);
t2.start();
}
}
class T1 extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 1000; i++) {
System.out.println("T1:==="+i);
}
}
}
class T2 extends Thread{
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
System.out.println("T2:==="+i);
}
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
package org.demo.thread;
public class Demo4 extends Thread{
Timer timer=new Timer();
@Override
public void run() {
// TODO Auto-generated method stub
timer.add(Thread.currentThread().getName());
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Demo4 d1=new Demo4();
d1.setName("d1");
Demo4 d2=new Demo4();
d2.setName("d2");
d1.start();
d2.start();
}
}
class Timer{
private static int num=0;
//线程独占资源,也称线程枷锁,synchronized 独占对象
public synchronized void add(String name){
num++;
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(name+"你是第"+num+"使用Timer的线程");
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
package org.demo.thread;
public class Demo6 implements Runnable{
private int num=100;
@Override
public void run() {
// TODO Auto-generated method stub
display();
}
/**
* 此案列主要说明 线程独占资源的时候,没有被synchronized 的其他线程是可以访问的,请看num打印的值
*/
public synchronized void display(){
num=1000;
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("num:="+num);
}
public void test(){
try {
Thread.sleep(2500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
num=2000;
System.out.println(num);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Demo6 d1=new Demo6();
Thread t1=new Thread(d1);
t1.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
d1.test();
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
package org.demo.thread;
public class DemoLock implements Runnable{
public int flag=1;
private Object o1=new Object(),o2=new Object();
@Override
public void run() {
System.out.println("flag:"+flag);
if(flag==1){
synchronized(o1){
// try {
// Thread.sleep(500);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
synchronized(o2){
System.out.println("-----o1");
}
}
}
if(flag==0){
synchronized(o2){
// try {
// Thread.sleep(500);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
synchronized(o1){
System.out.println("----------o2");
}
}
}
}
/**
* 此案列主要讲述 线程死锁的案列
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DemoLock k1=new DemoLock();
DemoLock k2=new DemoLock();
k1.flag=1;
k2.flag=0;
Thread t1=new Thread(k1);
Thread t2=new Thread(k2);
t1.start();
t2.start();
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------