Arvutiteaduse instituut
  1. Kursused
  2. 2023/24 sügis
  3. Paralleelprogrammeerimise keeled (LTAT.06.022)
EN
Logi sisse

Paralleelprogrammeerimise keeled 2023/24 sügis

  • Pealeht
  • Loengud
  • Labs
  • Viited
  • Homework

Java (parallel) streams

Use the Java tutorial on Aggregate Operations as a reference if you are unsure.

  1. Given a list of names like (william, jones, aaron, bob, frank, gillian), calculate using streams the sum of the letters of all names longer than 4 letters.
  2. Calculate the sum of all even numbers from 2 to a given number N using streams. Ideally, do not manually generate the list of numbers.
  3. Given a class Person as below, print the age of the oldest person in a list of Persons using streams.
  4. Using the same class, print the name of the oldest person in a list of Persons using streams.

class Person {

	private String name;
	private int age;
	private String nationality;

	public Person(String name, int age, String nationality) {
		this.name = name;
		this.age = age;
		this.nationality = nationality;
	}

	public Person(String name, int age) {
		this(name, age, "");
	}

	public String getName() {
		return name;
	}

	public int getAge() {
		return age;
	}

  public String getNationality() {
    return nationality;
  }
}

Parallel streams doing real work

Most of this is from lab 3, the description is repeated for convenience.

We want to find the number of divisors of the number in a range of positive numbers which has the maximum number of divisors. A divisor is a number which divides another number with remainder 0. For example, 9 has two divisors, namely 3 and 1, whereas 8 has three, namely 1,2, and 4 (if the 1 is included or not does not matter for the maximum). Using the int data type should be enough for this exercise.

As a first step, write a program which finds the divisors for a number.

Then extend this to find the maximum number of divisors for a range in a sequential fashion. Try this out for eg the range from 1 to 1000 and 1 to 100,000 and 1 to 10,000,000. Record how long it takes for 1 to 10,000,000.

Finally, implement a concurrent version using parallel streams. How much of an speedup do we have?

  • Arvutiteaduse instituut
  • Loodus- ja täppisteaduste valdkond
  • Tartu Ülikool
Tehniliste probleemide või küsimuste korral kirjuta:

Kursuse sisu ja korralduslike küsimustega pöörduge kursuse korraldajate poole.
Õppematerjalide varalised autoriõigused kuuluvad Tartu Ülikoolile. Õppematerjalide kasutamine on lubatud autoriõiguse seaduses ettenähtud teose vaba kasutamise eesmärkidel ja tingimustel. Õppematerjalide kasutamisel on kasutaja kohustatud viitama õppematerjalide autorile.
Õppematerjalide kasutamine muudel eesmärkidel on lubatud ainult Tartu Ülikooli eelneval kirjalikul nõusolekul.
Courses’i keskkonna kasutustingimused