进程是指,在内存中运行的应用程序,每个进程都有自己独立的一块内存空间,一个应用程序可以同时启动多个进程。

另外:

1.进程

代码示例:进程的操作

 1 package org.wanglei._02process;
 2 
 3 public class ProcessDemo {
 4 
 5     /**
 6      * 操作进程,用了两个方法。
 7      * @param args
 8      * @throws Exception 
 9      */
10     public static void main(String[] args) throws Exception {
11         //方法一:使用了Runtime类的exec方法
12         Runtime rt = Runtime.getRuntime();
13         rt.exec("notepad");
14         //方法二:使用了ProcessBuilder类的start方法
15         ProcessBuilder pb = new ProcessBuilder("notepad");
16         pb.start();
17 
18     }
19 
20 }

2.线程

  创建和执行线程的两种方式

两种方式的区别

  总结:实现方式可以共享同一个资源,继承方式不行。

  但是:必须保证线程同步,才可以正常共享一个资源。

  方法2的语法:

                                

 

 

注意一点:

            

   使用synchronized时,尽量减少作用域!

 

 1 package com.wang.syn;
 2 
 3 class Syn {
 4 
 5     public void testSyn() {
 6         synchronized (Syn.class) {
 7             System.out.println(Thread.currentThread().getName() + "  start...");
 8             try {
 9                 Thread.sleep(1000);
10             } catch (Exception e) {
11                 e.printStackTrace();
12             }
13             System.out.println(Thread.currentThread().getName() + "  end...");
14         }
15     }
16 }
17 
18 class MyThread implements Runnable {
19 
20     @Override
21     public void run() {
22         Syn s = new Syn();
23         s.testSyn();
24     }
25 
26 }
27 
28 public class Main {
29 
30     public static void main(String[] args) {
31         for (int i = 0; i < 3; i++) {
32             MyThread myThread = new MyThread();
33             new Thread(myThread).start();
34         }
35     }
36 }

Thread-1 start...
Thread-1 end...
Thread-0 start...
Thread-0 end...
Thread-2 start...
Thread-2 end...

以上代码中,可以保证线程同步。

线程优先级代码示例:

 1 package operate_thread;
 2 class PriorityThread extends Thread{
 3     public PriorityThread(String typeName) {
 4         super(typeName);
 5     }
 6 
 7     public void run() {
 8         for (int i = 0; i < 10; i++) {
 9             System.out.println(super.getName() + i);
10         }
11     }
12 }
13 public class PriorityDemo {
14 
15     /**
16      * @param args
17      */
18     public static void main(String[] args) {
19         PriorityThread max = new PriorityThread("高优先级");
20         max.setPriority(Thread.MAX_PRIORITY);
21         
22         PriorityThread min = new PriorityThread("低优先级");
23         min.setPriority(Thread.MIN_PRIORITY);
24         
25         min.start();
26         max.start();
27         
28         
29     }
30 
31 }

低优先级0
高优先级0
低优先级1
高优先级1
低优先级2
高优先级2
低优先级3
高优先级3
低优先级4
高优先级4
低优先级5
高优先级5
低优先级6
高优先级6
高优先级7
高优先级8
高优先级9
低优先级7
低优先级8
低优先级9

----------------------------------

同步锁代码演示:

 1 package com.wang.lock;
 2 
 3 import java.util.concurrent.locks.Lock;
 4 import java.util.concurrent.locks.ReentrantLock;
 5 
 6 public class LockDemo implements Runnable {
 7 
 8     /**
 9      * 演示同步锁lock
10      * @param args
11      */
12     public static void main(String[] args) {
13          LockDemo lockDemo = new LockDemo();
14          new Thread(lockDemo).start();
15          new Thread(lockDemo).start();
16          new Thread(lockDemo).start();
17     }
18 
19     private int num = 50;
20     private final Lock lock = new ReentrantLock();
21 
22     @Override
23     public void run() {
24         for (int i = 0; i < 50; i++) {
25             use();
26         }
27     }
28 
29     private void use() {
30         lock.lock();
31         try {
32             if (num > 0) {
33                 System.out.println(Thread.currentThread().getName() + "使用了一张票"
34                         + num);
35                 Thread.sleep(100);
36                 num--;
37             }
38         } catch (Exception e) {
39             e.printStackTrace();
40         } finally {
41             lock.unlock();
42         }
43     }
44 
45 }

Thread-0使用了一张票50
Thread-0使用了一张票49
Thread-0使用了一张票48
Thread-0使用了一张票47
Thread-0使用了一张票46
Thread-0使用了一张票45
Thread-0使用了一张票44
Thread-0使用了一张票43
Thread-0使用了一张票42
Thread-1使用了一张票41
Thread-1使用了一张票40
Thread-2使用了一张票39
Thread-2使用了一张票38
Thread-0使用了一张票37
Thread-0使用了一张票36
Thread-0使用了一张票35
Thread-0使用了一张票34
Thread-0使用了一张票33
Thread-0使用了一张票32
Thread-0使用了一张票31
Thread-0使用了一张票30
Thread-0使用了一张票29
Thread-0使用了一张票28
Thread-0使用了一张票27
Thread-0使用了一张票26
Thread-0使用了一张票25
Thread-0使用了一张票24
Thread-0使用了一张票23
Thread-0使用了一张票22
Thread-0使用了一张票21
Thread-0使用了一张票20
Thread-0使用了一张票19
Thread-0使用了一张票18
Thread-0使用了一张票17
Thread-0使用了一张票16
Thread-0使用了一张票15
Thread-0使用了一张票14
Thread-0使用了一张票13
Thread-0使用了一张票12
Thread-0使用了一张票11
Thread-0使用了一张票10
Thread-0使用了一张票9
Thread-0使用了一张票8
Thread-0使用了一张票7
Thread-0使用了一张票6
Thread-0使用了一张票5
Thread-0使用了一张票4
Thread-0使用了一张票3
Thread-1使用了一张票2
Thread-1使用了一张票1

 3.线程通信

  举一个生产者和消费者的案例。

  1.首先需要保证,生产者的生产动作、消费者的消费动作独立进行,两者不受干扰,就得使用同步操作

  2.还需要保证,生产者和消费者交替进行,就得使用等待和唤醒机制

  

  同步锁:

  

  所以,线程之间的通信:使用wait()和notify()方法————被线程共享的X对象来调用(this)

   示例代码:

  Resource:

package com.wl.thread;

public class Resource {
    private String name;
    private String gender;
    private boolean isEmpty = true;

    public synchronized void push(String name, String gender) {
        try {
            while (!isEmpty) {
                this.wait();// 此时有数据,则让生产者线程wait,释放同步锁,等待消费者消费
            }
            this.name = name;
            Thread.sleep(100);
            this.gender = gender;
            
            isEmpty = false;
            this.notify();// 生产完毕后去唤醒消费者
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public synchronized void popup() {
        try {
            while (isEmpty) {
                this.wait();
            }
            Thread.sleep(100);
            System.out.println(this.name + "-----" + this.gender);
            isEmpty = true;
            this.notify();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

producer:

 1 package com.wl.thread;
 2 
 3 public class Producer implements Runnable {
 4     private Resource r = null;
 5 
 6     public Producer(Resource r) {
 7         super();
 8         this.r = r;
 9     }
10 
11     @Override
12     public void run() {
13         for (int i = 0; i < 20; i++) {
14             if (i % 2 == 0) {
15                 r.push("A", "1");
16             } else {
17                 r.push("B", "2");
18             }
19         }
20     }
21 }

consumer:

 1 package com.wl.thread;
 2 
 3 public class Consumer implements Runnable {
 4 
 5     private Resource r = null;
 6 
 7     public Consumer(Resource r) {
 8         super();
 9         this.r = r;
10     }
11 
12     @Override
13     public void run() {
14         for (int i = 0; i < 20; i++) {
15             r.popup();
16         }
17     }
18 }

App:

 1 package com.wl.thread;
 2 
 3 public class App {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         Resource r = new Resource();
10         Thread t = new Thread(new Producer(r));
11         t.start();
12         Thread t2 = new Thread(new Consumer(r));
13         t2.start();
14     }
15 }

A-----1
B-----2
A-----1
B-----2
A-----1
B-----2
A-----1
B-----2
A-----1
B-----2
A-----1
B-----2
A-----1
B-----2
A-----1
B-----2
A-----1
B-----2
A-----1
B-----2

-------------------------------------达到了同步并且交替执行

  lock的线程通信:

  总结:使用condition.await()    condition.signal()替换

 

posted on 2017-08-04 15:48  海豚汪洋  阅读(137)  评论(0编辑  收藏  举报