`
DavyJones2010
  • 浏览: 156325 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Java Concurrency: Thread Introduction

阅读更多

1) Sleep

package edu.xmu.thread;

public class SleepTest
{
    public static void main(String[] args)
    {
        Thread thread1 = new Thread(new MyRunnable());
        Thread thread2 = new Thread(new MyRunnable());
        Thread thread3 = new Thread(new MyRunnable());

        System.out.println(System.currentTimeMillis());
        thread1.start();
        thread2.start();
        thread3.start();
        System.out.println(System.currentTimeMillis());
    }

    private static class MyRunnable implements Runnable
    {
        @Override
        public void run()
        {
            try
            {
                System.out.println(Thread.currentThread().getName()
                        + " is going to sleep.");
                Thread.sleep(1000);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }

    }
}
    Output:
1400034838666
Thread-0 is going to sleep.
1400034838666
Thread-1 is going to sleep.
Thread-2 is going to sleep.
// We can see that static method Thread.sleep() will sleep the current thread.
    Attention:
System.out.println(System.currentTimeMillis());
thread1.start();
thread2.start();
thread3.start();
thread3.sleep(1000);
System.out.println(System.currentTimeMillis());

// Output:
//1400035177744
//Thread-0 is going to sleep.
//Thread-1 is going to sleep.
//Thread-2 is going to sleep.
//1400035178744
// The thread3.sleep(1000); will not make thread3 sleep.
// It will make current main thread sleep instead.
// So we can find out that although threadInstance.sleep() is applicable,
// it will make the current thread instead of threadInstance sleep.
// Pay attention to this pitfall.
 

2) Join

    Example below:

package edu.xmu.thread;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

public class ThreadTest
{
    public static void main(String[] args)
    {
        List<CalculationThread> subThreadList = initSubThreadList();
        List<Integer> numList = new ArrayList<Integer>();

        for (CalculationThread calThread : subThreadList)
        {
            calThread.start(); // Start all calculation thread
        }
        try
        {
            System.out.println("Current Time Mills: "
                    + Calendar.getInstance().getTimeInMillis());

            for (CalculationThread calThread : subThreadList)
            {
                calThread.join();
            }

            System.out.println("Current Time Mills: "
                    + Calendar.getInstance().getTimeInMillis());

            CalculationThread sumThread = new CalculationThread();
            for (CalculationThread subThread : subThreadList)
            {
                numList.add(subThread.getSum());
            }
            sumThread.setNumList(numList);
            sumThread.start();
            sumThread.join();
            System.out.println("Current Time Mills: "
                    + Calendar.getInstance().getTimeInMillis());
            System.out.println(sumThread.getSum());
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }

    }

    private static List<CalculationThread> initSubThreadList()
    {
        CalculationThread thread1 = new CalculationThread(Arrays.asList(200,
                200, 200, 200));
        CalculationThread thread2 = new CalculationThread(Arrays.asList(300,
                300, 300, 300));
        CalculationThread thread3 = new CalculationThread(Arrays.asList(300,
                300, 300, 300));
        CalculationThread thread4 = new CalculationThread(Arrays.asList(300,
                300, 300, 300));
        CalculationThread thread5 = new CalculationThread(Arrays.asList(300,
                300, 300, 300));

        List<CalculationThread> subThreadList = new ArrayList<CalculationThread>();
        subThreadList.add(thread1);
        subThreadList.add(thread2);
        subThreadList.add(thread3);
        subThreadList.add(thread4);
        subThreadList.add(thread5);

        return subThreadList;
    }
}

class CalculationThread extends Thread
{
    private List<Integer> numList;

    private int sum;

    public CalculationThread()
    {
        super();
    }

    public CalculationThread(List<Integer> numList)
    {
        super();
        this.numList = numList;
    }

    @Override
    public void run()
    {
        try
        {
            Thread.sleep(2000);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        for (int i : numList)
        {
            sum += i;
        }
    }

    public int getSum()
    {
        return sum;
    }

    public void setNumList(List<Integer> numList)
    {
        this.numList = numList;
    }

}

    Output:

Current Time Mills: 1400033660648
Current Time Mills: 1400033662639
Current Time Mills: 1400033664640
5600
// We can find out the total subCalculation time cost is: 2000ms.
// So we can use this kind of technique(join method) to divide 
// a huge task into several sub-task and start all the sub-task at once
// The main thread will wait till all the sub-task done and then gather
// all the result together.

  

    Example2: For special requirement that a list of threads run sequentially.

package edu.xmu.thread;

public class SequenceThreadTest {

    public static void main(String[] args) throws InterruptedException {

	Thread sequenceThread = new Thread(new SequenceThread(),
		"sequenceThread");
	Thread sequenceThread2 = new Thread(new SequenceThread(),
		"sequenceThread2");
	Thread sequenceThread3 = new Thread(new SequenceThread(),
		"sequenceThread3");
	Thread sequenceThread4 = new Thread(new SequenceThread(),
		"sequenceThread4");

	sequenceThread.start();
	sequenceThread.join(); // The caller thread(Main thread) will block until sequenceThread finished.
	sequenceThread2.start();
	sequenceThread2.join();// The caller thread(Main thread) will block until sequenceThread2 finished.
	sequenceThread3.start();
	sequenceThread3.join();// The caller thread(Main thread) will block until sequenceThread3 finished.
	sequenceThread4.start();
	sequenceThread4.join();// The caller thread(Main thread) will block until sequenceThread4 finished.
	System.out.println("Finished");
    }
}

class SequenceThread implements Runnable {
    @Override
    public void run() {
	try {
	    Thread.sleep((long) (1000 * Math.random()));
	    System.out
		    .println("Thread: " + Thread.currentThread()
			    + " is running(). Timestamp: "
			    + System.currentTimeMillis());
	} catch (InterruptedException e) {
	    e.printStackTrace();
	}
    }
}

    Output:

Thread: Thread[sequenceThread,5,main] is running(). Timestamp: 1401247052161
Thread: Thread[sequenceThread2,5,main] is running(). Timestamp: 1401247052625
Thread: Thread[sequenceThread3,5,main] is running(). Timestamp: 1401247053363
Thread: Thread[sequenceThread4,5,main] is running(). Timestamp: 1401247054014
Finished

    Another Approach:

package edu.xmu.thread;

public class SequenceThreadTest {

    public static void main(String[] args) throws InterruptedException {

	Thread sequenceThread = new Thread(new SequenceThread(null),
		"sequenceThread");
	Thread sequenceThread2 = new Thread(new SequenceThread(sequenceThread),
		"sequenceThread2");
	Thread sequenceThread3 = new Thread(
		new SequenceThread(sequenceThread2), "sequenceThread3");
	Thread sequenceThread4 = new Thread(
		new SequenceThread(sequenceThread3), "sequenceThread4");

	sequenceThread.start();
	System.out.println("sequenceThread started. Timestamp: "
		+ System.currentTimeMillis());
	sequenceThread2.start();
	System.out.println("sequenceThread2 started. Timestamp: "
		+ System.currentTimeMillis());
	sequenceThread3.start();
	System.out.println("sequenceThread3 started. Timestamp: "
		+ System.currentTimeMillis());
	sequenceThread4.start();
	System.out.println("sequenceThread4 started. Timestamp: "
		+ System.currentTimeMillis());
	sequenceThread4.join();
	System.out
		.println("Finished. Timestamp: " + System.currentTimeMillis());
    }
}

class SequenceThread implements Runnable {
    Thread previousThread;

    public SequenceThread(Thread previousThread) {
	super();
	this.previousThread = previousThread;
    }

    @Override
    public void run() {
	try {
	    if (previousThread != null) {
		previousThread.join(); // The caller thread(current thread) will block until previousThread finished. And main thread will not blocked.
	    }
	    Thread.sleep((long) (1000 * Math.random()));
	    System.out
		    .println("Thread: " + Thread.currentThread()
			    + " is running(). Timestamp: "
			    + System.currentTimeMillis());
	} catch (InterruptedException e) {
	    e.printStackTrace();
	}
    }
}

    Output:

sequenceThread started. Timestamp: 1401247481556
sequenceThread2 started. Timestamp: 1401247481556
sequenceThread3 started. Timestamp: 1401247481556
sequenceThread4 started. Timestamp: 1401247481556
Thread: Thread[sequenceThread,5,main] is running(). Timestamp: 1401247482485
Thread: Thread[sequenceThread2,5,main] is running(). Timestamp: 1401247482935
Thread: Thread[sequenceThread3,5,main] is running(). Timestamp: 1401247483014
Thread: Thread[sequenceThread4,5,main] is running(). Timestamp: 1401247483916
Finished. Timestamp: 1401247483916

// We can see that MainThread will not block.

 

分享到:
评论

相关推荐

    Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams

    Get an easy introduction to reactive streams in Java to handle concurrency, data streams, and the propagation of change in today's applications. This compact book includes in-depth introductions to ...

    Java - The Well-Grounded Java Developer

    - **Overview**: Explores modern concurrency patterns and APIs available in Java, focusing on efficient parallel processing and thread safety. - **Key Concepts**: - **Java Memory Model (JMM)**: ...

    java.核心技术.第八版 卷1

    GUI programming with Swing, exception handling, generics, collections, and concurrency–look for the eighth edition of Core Java. Volume I–Fundamentals (ISBN: 978-0-13-235476-9).

    Top_10_Java_Performance_Problems

    #### Introduction 在Java应用程序开发过程中,性能问题是一个常见的挑战。这些问题不仅影响应用的响应时间和资源利用率,还可能影响用户体验和业务效率。本文将详细介绍十个最常见的Java性能问题,并提供相应的解决...

    AkkaScala.pdf

    #### Introduction to Akka **Akka** is a toolkit and runtime designed for building highly concurrent, distributed, and fault-tolerant systems on the Java Virtual Machine (JVM). It leverages the actor ...

    python3.6.5参考手册 chm

    Python参考手册,官方正式版参考手册,chm版。以下摘取部分内容:Navigation index modules | next | Python » 3.6.5 Documentation » Python Documentation contents What’s New in Python ...

Global site tag (gtag.js) - Google Analytics