Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.URL;
- import java.util.Locale;
- public class DownloadExample {
- static int count = 0;
- // lista plików do pobrania
- static String [] toDownload = {
- "http://home.agh.edu.pl/pszwed/wyklad-c/01-jezyk-c-intro.pdf",
- "http://home.agh.edu.pl/~pszwed/wyklad-c/02-jezyk-c-podstawy-skladni.pdf",
- "http://home.agh.edu.pl/~pszwed/wyklad-c/03-jezyk-c-instrukcje.pdf",
- "http://home.agh.edu.pl/~pszwed/wyklad-c/04-jezyk-c-funkcje.pdf",
- "http://home.agh.edu.pl/~pszwed/wyklad-c/05-jezyk-c-deklaracje-typy.pdf",
- "http://home.agh.edu.pl/~pszwed/wyklad-c/06-jezyk-c-wskazniki.pdf",
- "http://home.agh.edu.pl/~pszwed/wyklad-c/07-jezyk-c-operatory.pdf",
- "http://home.agh.edu.pl/~pszwed/wyklad-c/08-jezyk-c-lancuchy-znakow.pdf",
- "http://home.agh.edu.pl/~pszwed/wyklad-c/09-jezyk-c-struktura-programow.pdf",
- "http://home.agh.edu.pl/~pszwed/wyklad-c/10-jezyk-c-dynamiczna-alokacja-pamieci.pdf",
- "http://home.agh.edu.pl/~pszwed/wyklad-c/11-jezyk-c-biblioteka-we-wy.pdf",
- "http://home.agh.edu.pl/~pszwed/wyklad-c/preprocesor-make-funkcje-biblioteczne.pdf",
- };
- static class Downloader implements Runnable{
- private final String url;
- Downloader(String url){
- this.url = url;
- }
- public void run(){
- String [] divided = url.split("/");
- String fileName = divided[divided.length - 1];
- try(InputStream in = new URL(url).openStream(); FileOutputStream out = new FileOutputStream(fileName) ){
- for(;;){
- // czytaj znak z in
- // jeśli <0 break
- int sign = in.read();
- if (sign < 0) break;
- //zapisz znak do out
- out.write((char)sign);
- }
- count++;
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("Done:"+fileName);
- }
- }
- static void sequentialDownload(){
- double t1 = System.nanoTime()/1e6;
- for(String url:toDownload){
- new Downloader(url).run();
- }
- double t2 = System.nanoTime()/1e6;
- System.out.printf(Locale.US,"t2-t1=%f\n",t2-t1);
- }
- static void concurrentDownload(){
- double t1 = System.nanoTime()/1e6;
- for(String url:toDownload){
- // uruchom Downloader jako wątek...
- Runnable runnable = new Downloader(url);
- Thread t = new Thread(runnable);
- t.start();
- }
- double t2 = System.nanoTime()/1e6;
- System.out.printf(Locale.US,"t2-t1=%f\n",t2-t1);
- }
- static void concurrentDownload2(){
- double t1 = System.nanoTime()/1e6;
- for(String url:toDownload){
- // uruchom Downloader jako wątek...
- Runnable runnable = new Downloader(url);
- Thread t = new Thread(runnable);
- t.start();
- }
- while(count!=toDownload.length){
- // wait...
- Thread.yield();
- }
- double t2 = System.nanoTime()/1e6;
- System.out.printf(Locale.US,"t2-t1=%f\n",t2-t1);
- }
- public static void main(String[] args) {
- //sequentialDownload();
- concurrentDownload2();
- }
- }
- /////////////////////////////////////
- import javax.swing.*;
- import java.awt.geom.Point2D;
- import java.time.LocalTime;
- import java.awt.*;
- import java.awt.geom.AffineTransform;
- public class ClockWithGui extends JPanel {
- LocalTime time = LocalTime.now();
- ClockWithGui(){
- ClockWithGui.ClockThread clock = new ClockWithGui.ClockThread();
- clock.start();
- }
- class ClockThread extends Thread{
- @Override
- public void run() {
- for(;;){
- time = LocalTime.now();
- System.out.printf("%02d:%02d.%02d \n",time.getHour(),time.getMinute(),time.getSecond());
- try {
- sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- repaint();
- setOpaque(false);
- }
- }
- }
- public void paintComponent(Graphics g){
- Graphics2D g2d=(Graphics2D)g;
- g2d.translate(getWidth()/2,getHeight()/2);
- for(int i=1;i<13;i++){
- AffineTransform at = new AffineTransform();
- at.rotate(2*Math.PI/12*i);
- Point2D src = new Point2D.Float(0,-120);
- Point2D trg = new Point2D.Float();
- at.transform(src,trg);
- g2d.drawString(Integer.toString(i),(int)trg.getX(),(int)trg.getY());
- }
- AffineTransform saveAT = g2d.getTransform();
- BasicStroke bs = new BasicStroke(2);
- // wskazowka sekundowa
- g2d.rotate(time.getSecond()%60*2*Math.PI/60);
- g2d.setStroke(bs);
- g2d.setColor(new Color(255,0,0));
- g2d.drawLine(0,0,0,-100);
- g2d.setTransform(saveAT);
- g2d.setColor(new Color(0,0,0));
- // wskazowka minutowa
- g2d.rotate(time.getMinute()%60*2*Math.PI/60);
- bs = new BasicStroke(5);
- g2d.setStroke(bs);
- g2d.drawLine(0,0,0,-80);
- g2d.setTransform(saveAT);
- // wskazowka godzinowa
- g2d.rotate(time.getHour()%12*2*Math.PI/12);
- bs = new BasicStroke(8);
- g2d.setStroke(bs);
- g2d.drawLine(0,0,0,-50);
- g2d.setTransform(saveAT);
- }
- public static void main(String[] args) {
- new ClockWithGui();
- JFrame frame = new JFrame("Clock");
- frame.setContentPane(new ClockWithGui());
- frame.setSize(700, 700);
- frame.setLocationRelativeTo(null);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setResizable(true);
- frame.setVisible(true);
- }
- }
- ///////////////////////
- import java.time.LocalTime;
- public class Clock extends Thread{
- @Override
- public void run() {
- while (true) {
- LocalTime time = LocalTime.now();
- System.out.printf("%02d:%02d.%02d\n",
- time.getHour(),
- time.getMinute(),
- time.getSecond());
- try {
- sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- public static void main(String[] args) {
- new Clock().start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement