Tải bản đầy đủ - 0 (trang)
15-9. Share Data Between Tasks

15-9. Share Data Between Tasks

Tải bản đầy đủ - 0trang

CHAPTER 15 ■ PARALLEL PROGRAMMING



techniques demonstrated in Chapter 4 will work with the .NET parallel programming model, but the

new collection classes may be more efficient for large-scale applications. Table 15-1 lists the most useful

classes from the System.Collections.Concurrent namespace.

Table 15-1. Useful System.Collections.Concurrent Classes



Class



Description



ConcurrentBag



A thread-safe collection of objects where no typing or ordering is assumed



ConcurrentDictionary



A key/value pair collection



ConcurrentQueue



A first in, first out (FIFO) queue



ConcurrentStack



A last in, first out (LIFO) stack



These new collections take care of managing data automatically—you do not have to use

synchronization techniques in your code.



The Code

The following example creates a ConcurrentStack, which is then used by three Tasks.

using

using

using

using

using

using

using



System;

System.Collections.Generic;

System.Linq;

System.Text;

System.Threading;

System.Threading.Tasks;

System.Collections.Concurrent;



namespace Recipe15_9

{

class Recipe15_9

{

static void Main(string[] args)

{

// Create a concurrent collection.

ConcurrentStack cStack = new ConcurrentStack();

// create tasks that will use the stack

Task task1 = Task.Factory.StartNew(

() => addNumbersToCollection(cStack));

Task task2 = Task.Factory.StartNew(

() => addNumbersToCollection(cStack));

Task task3 = Task.Factory.StartNew(

() => addNumbersToCollection(cStack));



746



www.it-ebooks.info



CHAPTER 15 ■ PARALLEL PROGRAMMING



// Wait for all of the tasks to complete.

Task.WaitAll(task1, task2, task3);

// Report how many items there are in the stack.

Console.WriteLine("There are {0} items in the collection",

cStack.Count);

// Wait to continue.

Console.WriteLine("\nMain method complete. Press Enter");

Console.ReadLine();

}

static void addNumbersToCollection(ConcurrentStack stack)

{

for (int i = 0; i < 1000; i++)

{

stack.Push(i);

}

}

}

}



747



www.it-ebooks.info



www.it-ebooks.info



C H A P T E R 16

■■■



Using LINQ

In some of the previous chapters, we illustrated how to use LINQ to perform queries on different types of

data. Chapter 2 showed how to query collections and arrays, Chapter 6 to query XML trees, and Chapter

9 to query databases. This chapter shows you how to build on those simple examples to exploit the full

flexibility of LINQ.

One of the best features of LINQ is that you can perform the same kinds of queries whatever the data

source is. Each of the recipes in this chapter uses an array or a collection as the data source, but the same

techniques can be applied equally to XML or databases. The recipes in this chapter are all self-contained

and illustrate different LINQ features—but part of the allure of LINQ is that you will be able to combine

these techniques to create complex and powerful queries. The recipes in this chapter describe how to

perform the following tasks:





Filter elements in a data source (recipes 16-1, 16-2, and 16-3)







Create anonymous types in results (recipe 16-4)







Work with multiple data sources in queries (recipes 16-6, 16-7, ad 16-8)







Grouping, sorting, comparing, and aggregating data (recipes 16-9 through to 1612)







Sharing interim results across query clauses (recipe 16-13)







Extending LINQ with custom extension methods (recipe 16-14)







Converting LINQ results into other types (recipe 16-15)



16-1. Perform a Simple LINQ Query

Problem

You need to select all items from a collection, database, or XML document.



749



www.it-ebooks.info



CHAPTER 16 ■ USING LINQ



Solution

Use the from and select keywords.



How It Works

The most basic LINQ query selects all of the items contained in a data source. The most powerful aspect

of LINQ is that you can apply the same query approach to any data source and get consistent,

predictable results. Microsoft has embedded LINQ support throughout the .NET Framework so that you

can use arrays, collections, XML documents and databases in the same way.

To select all of the items in a data source is a simple two-step process

1.



Start a new LINQ query using the from keyword, providing an element variable

name that you will use to refer to elements that LINQ finds (for example, from

e in datasource).



2.



Indicate what will be added to the result set from each matching element using

the select keyword.



For the basic “select all” query used in this recipe, we simply define the element variable name in

the first step and use it as the basis for the second step, as follows:

IEnumerable myEnum = from e in datasource select e;

The type that you use for the datasource reference must implement the System.Collections.

Generic.IEnumerable<> interface. If you are using an array or a generic collection, then you can simply

use the references to your instance as the data source because arrays and all standard generic collections

implement IEnumerable<>, as follows:

IEnumerable myEnum = from e in myarray select e;

IEnumerable myEnum = from e in mycollection select e;

If you are using an XML tree, you can get an IEnumerable from the root XElement by calling the

Elements method; and for a DataTable, you can get an IEnumerable by calling the AsEnumerable method,

as follows:

IEnumerable myEnum = from e in root.Elements() select e;

IEnumerable myEnum = from e in table.AsEnumerable() select e;

Notice that the generic type of the result is dependent on the data source. For an array or collection,

the result will be an IEnumerable of the data contained in the array—string for string[] and

IList, for example. LINQ queries of DataTables return an IEnumerable, and queries of

an XML tree return IEnumerable.

If you want to select a value contained within a data source element (for example, if myType had a

property called Name that returned a string), then you simply specify the value you want after the select

keyword—for example:

IEnumerable myEnum = from e in datasource select e.Name;



750



www.it-ebooks.info



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

15-9. Share Data Between Tasks

Tải bản đầy đủ ngay(0 tr)

×