Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Да се имплементира класа FileScanner која што ќе се однесува како thread. Во класата FileScanner се чуваат податоци за : - патеката на директориумот што треба да се скенира - статичка променлива counter што ќе брои колку нишки од класата FileScanner ќе се креираат Во класата FileScanner да се имплементираа статички методот што ќе печати информации за некоја датотека од следниот формат:
- dir: C:\Users\185026\Desktop\lab1 - reshenija 4096 (dir за директориуми, апсолутна патека и големина)
- file: C:\Users\Stefan\Desktop\spisok.pdf 29198 (file за обични фајлови, апсолутна патека и големина)
- Дополнително да се преоптовари методот run() од класата Thread, така што ќе печати информации за директориумот за којшто е повикан. Доколку во директориумот има други под директориуми, да се креира нова нишка од тип FileScanner што ќе ги прави истите работи како и претходно за фајловите/директориумите што се наоѓаат во тие директориуми (рекурзивно).
- На крај да се испечати вредноста на counter-от, односно колку вкупно нишки биле креирани. Користете го следниот почетен код.
- */
- public class FileScanner {
- private String fileToScan;
- //TODO: Initialize the start value of the counter
- private static Long counter;
- public FileScanner (String fileToScan) {
- this.fileToScan=fileToScan;
- //TODO: Increment the counter on every creation of FileScanner object
- }
- public static void printInfo(File file) {
- /*
- * TODO: Print the info for the @argument File file, according to the requirement of the task
- * */
- }
- public static Long getCounter () {
- return counter;
- }
- public void run() {
- //TODO Create object File with the absolute path fileToScan.
- File file;
- //TODO Create a list of all the files that are in the directory file.
- File [] files = null;
- for (File f : files) {
- /*
- * TODO If the File f is not a directory, print its info using the function printInfo(f)
- * */
- /*
- * TODO If the File f is a directory, create a thread from type FileScanner and start it.
- * */
- //TODO: wait for all the FileScanner-s to finish
- }
- }
- public static void main (String [] args) {
- String FILE_TO_SCAN = "C:\\Users\\189075\\Desktop\\lab";
- //TODO Construct a FileScanner object with the fileToScan = FILE_TO_SCAN
- FileScanner fileScanner;
- //TODO Start the thread from type FileScanner
- //TODO wait for the fileScanner to finish
- //TODO print a message that displays the number of thread that were created
- }
- }
- ===================================######################################====================================
- ===================================######################################====================================
- /*
- Со помош на синхронизациските методи да се реши проблемот за определување на бројот на појавувања на бројот 7 во огромна низа и негово запишување во глобална променлива count.
- Секвенцијалното решение не е прифатливо поради тоа што трае многу долго време (поради големината на низата). За таа цел, потребно е да се паралелизира овој процес, при што треба да се напише метода која ќе ги брои појавувањата на бројот 7 во помал фрагмент од низата, при што резултатот повторно се чува во глобалната заедничка променлива count.
- Напомена: Почетниот код е даден во почетниот код CountSeven. Задачата да се тестира над низа од минимум 1000 елементи.
- */
- public class CountSeven {
- public static int NUM_RUNS = 100;
- /**
- * Promenlivata koja treba da go sodrzi brojot na pojavuvanja na elementot 7
- */
- int count = 0;
- /**
- * TODO: definirajte gi potrebnite elementi za sinhronizacija
- */
- public void init() {
- }
- class Counter extends Thread {
- public void count(int[] data) throws InterruptedException {
- // da se implementira
- }
- private int[] data;
- public Counter(int[] data) {
- this.data = data;
- }
- @Override
- public void run() {
- try {
- count(data);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- public static void main(String[] args) {
- try {
- CountSeven environment = new CountSeven();
- environment.start();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- public void start() throws Exception {
- init();
- HashSet<Thread> threads = new HashSet<Thread>();
- Scanner s = new Scanner(System.in);
- int total=s.nextInt();
- for (int i = 0; i < NUM_RUNS; i++) {
- int[] data = new int[total];
- for (int j = 0; j < total; j++) {
- data[j] = s.nextInt();
- }
- Counter c = new Counter(data);
- threads.add(c);
- }
- for (Thread t : threads) {
- t.start();
- }
- for (Thread t : threads) {
- t.join();
- }
- System.out.println(count);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement