@liruiyi962464
2017-03-22T03:42:31.000000Z
字数 6849
阅读 437
java
- 进程里边有多个线程
- CPU在同一个时间点上只有一个线程在工作
- CPU只管理资源,线程的进入是随机的
- 共享
- 并发
- 随机
- 普通java类继承自Therad类,就成为一个线程类,并且可以通过该类的start()方法来启动线程
- Therad类的子类可以直接实例化,但是要从写run()方法
public class Test1_Therad {
public static void main(String[] args) {
MyThread mt = new MyThread();
mt.start();
for(int i = 0;i<5;i++){
System.out.println("主方法。");
}
//匿名内部类相当于父类的子类
new Thread(){
public void run(){
for(int i = 0;i<5;i++){
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("匿名内部类");
}
}
}.start();
//new.MyThread().start();
}
}
class MyThread extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0;i<5;i++){
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("先定一个小目标123456789");
}
super.run();
}
}
public class Task3_3Thread {
public static void main(String[] args) {
TrainThread tt1 = new TrainThread();
tt1.setName("火车1");
tt1.start();
}
}
class TrainThread extends Thread{
@Override
public synchronized void run() {
// TODO Auto-generated method stub
synchronized (TrainThread.class) {
System.out.println(Thread.currentThread().getName()+"通过山洞");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
- 实现Runnable接口必须借助Thread类才能创建线程
1.创建Runnable接口的类的实例
2.创建一个Thread对象,将第一步实例化得到的Runnable对象作为参数传入Thread类的构造犯法- 通过Thread类的start()方法启动线程
public class Test2_Runnable {
public static void main(String[] args) {
MyRunnable mr = new MyRunnable();
Thread t = new Thread(mr);
t.start();
}
}
class MyRunnable implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0;i<5;i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Runnable");
}
}
}
public class Task3_3 {
public static void main(String[] args) {
TrainThread tt = new TrainThread();
Thread t1 = new Thread(tt, "火车1");
t1.start();
}
}
class TrainThread implements Runnable{
@Override
public synchronized void run() {
// TODO Auto-generated method stub
System.out.println(Thread.currentThread().getName()+"通过山洞");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class Test3_ThreadFiled {
public static void main(String[] args) {
new Thread("线程a"){
public void run() {
System.out.println(getName()+"........1..aaa...."+getId());
}
}.start();
new Thread("线程b"){
public void run() {
System.out.println(getName()+".........2...bbb.."+getId());
}
}.start();
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("没有设置/获取名字Runnable......3........."+Thread.currentThread().getId());
//currentThread()返回当前线程的引用
//Thread.currentThread().setName("线程c");设置线程的名字
System.out.println(Thread.currentThread().getName()+":设置/获取名字........4...Runnable......"+Thread.currentThread().getId());
}
},"线程c").start();//在这里也可以线程名字
Thread.currentThread().setName("我是主线程");
System.out.println(Thread.currentThread().getName()+"...........5............"+Thread.currentThread().getId());
}
}
- 直接在方法中添加关键字也可以加锁synchronized
- synchronized运行完之后再换/加锁
package com.xianchengtongbu;
public class Test_Synchronized {
public static void main(String[] args) {
Printer p = new Printer();
new Thread(){
public void run() {
while(true){
p.print1();
}};
}.start();
new Thread(){
public void run() {
while(true){
p.print2();
}
};}.start();
}
}
class Printer{
Object obg = new Object();
//直接在方法中添加关键字也可以加锁synchronized
public void print1() {//线程同步
//如果同步非静态方法,锁对象是this
//如果同步静态方法,锁对象是.class
synchronized (obg) {//synchronized运行完之后再换/加锁
System.out.print("每");
System.out.print("天");
System.out.print("进");
System.out.print("步");
System.out.print("一");
System.out.print("点");
System.out.print("点");
System.out.println();
}
}//锁对象是this
public void print2() {//线程不同步
System.out.print("好");
System.out.print("好");
System.out.print("学");
System.out.print("习");
System.out.println();
}
}
- 产生的原因:俩个线程都需要同一个对象,这个互相等待对方释放锁的过程,会导致死锁
public class Test1_SiSuo {
// 创建俩个筷子
private static String s1 = "筷子左";
private static String s2 = "筷子右";
public static void main(String[] args) {
// 匿名内部类相当于父类的子类
new Thread("皮皮虾") {
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
synchronized (s1) {
System.out.println(this.getName()+"获得"+s1+"等待"+s2);
synchronized (s2) {
System.out.println(this.getName()+"获得"+s2);
}
}
}
}
}.start();
new Thread("PPD") {
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
synchronized (s2) {
System.out.println(this.getName()+"获得"+s2+"等待"+s1);
synchronized (s1) {
System.out.println(this.getName()+"获得"+s1);
}
}
}
}
}.start();
}
}
- wait(): 在其他线程调用此对象的 notify() 方法或 notifyAll() 方法前,导致当前线程等待。
- notify():唤醒在此对象监视器上等待的单个线程。
- notifyAll(): 唤醒在此对象监视器上等待的所有线程。
public class Test1_Tongxin {
public static void main(String[] args) {
Printer p = new Printer();
new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
try {
sleep(1000);
p.print1();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
try {
sleep(1000);
p.print2();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
}
}
class Printer{
private int n = 1;
public void print1() throws Exception{
synchronized (this) {
if(n!=1){
this.wait();//等待
}
System.out.print("每");
System.out.print("天");
System.out.print("进");
System.out.print("步");
System.out.print("一");
System.out.print("点");
System.out.print("点");
System.out.println();
n = 2;
this.notify();//唤醒
}
}
public void print2() throws Exception{
synchronized (this) {
if(n!=2){
this.wait();
}
System.out.print("好");
System.out.print("好");
System.out.print("学");
System.out.print("习");
System.out.println();
n = 1;
this.notify();
}
}
}
public class Test1_Tongxin {
public static void main(String[] args) {
Printer p = new Printer();
new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
try {
sleep(1000);
p.print1();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
try {
sleep(1000);
p.print2();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
try {
sleep(1000);
p.print3();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
}
}
class Printer{
private int n = 1;
public void print1() throws Exception{
synchronized (this) {
while(n!=1){
this.wait();//等待
}
System.out.print("每");
System.out.print("天");
System.out.print("进");
System.out.print("步");
System.out.print("一");
System.out.print("点");
System.out.print("点");
System.out.println();
n = 2;
this.notifyAll();//唤醒其它线程
}
}
public void print2() throws Exception{
synchronized (this) {
while(n!=2){
this.wait();//等待
}
System.out.print("好");
System.out.print("好");
System.out.print("学");
System.out.print("习");
System.out.println();
n = 3;
this.notifyAll();//唤醒其它线程
}
}
public void print3() throws Exception{
synchronized (this) {
while(n!=3){
this.wait();//等待
}
System.out.print("天");
System.out.print("天");
System.out.print("向");
System.out.print("上");
System.out.println();
n = 1;
this.notifyAll();//唤醒其它线程
}
}
}