package linking;
public class duoxiancheng {
public static void main(String args[])
{
xiancheng test2=new xiancheng("a1");
xiancheng test6=new xiancheng("a2");
Runnable test3=new xiancheng2();
Thread test4=new Thread(test3,"b1");
Thread test5=new Thread(test3,"b2");
test2.start();
test6.start();
System.out.println("this is mian,start");
//test4.start();
// test5.start();
// try {
// test5.join();
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
System.out.println("this is main ,stop");
//这里的main是首先启动;
}
}
//继承自thread类
class xiancheng extends Thread
{
private String name;
public xiancheng(String name)
{
this.name=name;
}
private int b=10;
public void run()
{
for(int i=1;i<=10;i++)
{
System.out.println("FIRST "+name+" "+b--);
}
}
}
//实现runnable接口
class xiancheng2 implements Runnable
{
private int b=10;
@Override
public void run() {
for(int i=1;i<=10;i++)
{
System.out.println("2TH "+Thread.currentThread().getName()+b--);
}
}
1.无特殊情况下main线程都是首先执行
2:join()可以实现线程线程优先执行
3:yield()也可以实现线程优先,使线程进入runnable状态,实际测试效果不明显,取决与线程优先级和调度算法;
4:stop();线程进入睡眠;可以执行优先级低的线程;
5:interrupt();残暴;
6:wait;notify,synchronized;典型案例-打印;
wait与sleep的区别:1;object锁-thread类锁;
2:控制块-任何地方;
3:不需要捕获异常-捕获异常;
7:线程控制管理类
8:线程状态与切换;
9:线程值传递:构造方法,普通方法,object;
10:runnable可以实现lock;thread默认不能实现lock;
ps:以下代码转载自:http://blog.csdn.net/evankaka
public class MyThread1 extends Thread
{
private String name;
public MyThread1(String name)
{
this.name = name;
}
public void run()
{
System.out.println("hello " + name);
}
public static void main(String[] args)
{
Thread thread = new MyThread1("world");
thread.start();
}
}
//构造方法
public class MyThread2 implements Runnable
{
private String name;
public void setName(String name)
{
this.name = name;
}
public void run()
{
System.out.println("hello " + name);
}
public static void main(String[] args)
{
MyThread2 myThread = new MyThread2();
myThread.setName("world");
Thread thread = new Thread(myThread);
thread.start();
}
}
//一般方法
class Data
{
public int value = 0;
}
class Work
{
public void process(Data data, Integer numbers)
{
for (int n : numbers)
{
data.value += n;
}
}
}
public class MyThread3 extends Thread
{
private Work work;
public MyThread3(Work work)
{
this.work = work;
}
public void run()
{
java.util.Random random = new java.util.Random();
Data data = new Data();
int n1 = random.nextInt(1000);
int n2 = random.nextInt(2000);
int n3 = random.nextInt(3000);
work.process(data, n1, n2, n3); // 使用回调函数
System.out.println(String.valueOf(n1) + "+" + String.valueOf(n2) + "+"
+ String.valueOf(n3) + "=" + data.value);
}
public static void main(String[] args)
{
Thread thread = new MyThread3(new Work());
thread.start();
}
}
//object;
打印:
public class MyThreadPrinter2 implements Runnable {
private String name;
private Object prev;
private Object self;
private MyThreadPrinter2(String name, Object prev, Object self) {
this.name = name;
this.prev = prev;
this.self = self;
}
@Override
public void run() {
int count = 10;
while (count > 0) {
synchronized (prev) {
synchronized (self) {
System.out.print(name);
count--;
self.notify();
}
try {
prev.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) throws Exception {
Object a = new Object();
Object b = new Object();
Object c = new Object();
MyThreadPrinter2 pa = new MyThreadPrinter2("A", c, a);
MyThreadPrinter2 pb = new MyThreadPrinter2("B", a, b);
MyThreadPrinter2 pc = new MyThreadPrinter2("C", b, c);
new Thread(pa).start();
Thread.sleep(100); //确保按顺序A、B、C执行
new Thread(pb).start();
Thread.sleep(100);
new Thread(pc).start();
Thread.sleep(100);
}
}
ThreadB->ThreadC->ThreadA循环执行;
2个锁:自身锁,申请对方的锁;
leave:线程控制管理类;