I Have class to record sound in my android project .
In this class i have 2 function : start() and stop()
and i have a main class I want run it from other activity such : record(second)
and record some time and stop aoutomaticly after those second !
Like this :
start();
//sleep(second)
stop();
How I can do this ?
this is my code :
package com.example.voicerec;
import java.io.IOException;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Environment;
import android.view.View;
public class VoiceR {
private MediaRecorder myRecorder;
private MediaPlayer myPlayer;
private String outputFile = null;
public void Record()
{
outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/momeni.3gpp";
myRecorder = new MediaRecorder();
myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myRecorder.setAudioEncoder(MediaRecorder.OutputFormat.THREE_GPP);
myRecorder.setOutputFile(outputFile);
}
public void start(View view) {
try {
myRecorder.prepare();
myRecorder.start();
}
catch (IllegalStateException e) {
// start:it is called before prepare()
// prepare: it is called after start() or before setOutputFormat()
e.printStackTrace();
}
catch (IOException e) {
// prepare() fails
e.printStackTrace();
}
}
public void stop(View view) {
try {
myRecorder.stop();
myRecorder.release();
myRecorder = null;
}
catch (IllegalStateException e) {
// it is called before start()
e.printStackTrace();
}
catch (RuntimeException e) {
// no valid audio/video data has been received
e.printStackTrace();
}
}
public void play(View view) {
try {
myPlayer = new MediaPlayer();
myPlayer.setDataSource(outputFile);
myPlayer.prepare();
myPlayer.start();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void stopPlay(View view) {
try {
if (myPlayer != null) {
myPlayer.stop();
myPlayer.release();
myPlayer = null;
}
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Just make a thread that runs start then sleeps for record time then runs stop :
final int time = second * 1000;
Thread sound = new Thread(new Runnable() {
#Override
public void run() {
start(view);
try{
Thread.sleep(time);
}catch(Exception e){}
stop();
}
});
sound.start();
or you can use CountDownLatch :
CountDownLatch latch = new CountDownLatch(1);
start(view);
Thread stopping = new Thread(new Runnable() {
#Override
public void run() {
try {
latch.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
stop(view);
}
});
stopping.start();
and after the recording process finished:
latch.countDown();
Related
I have to make an assignment with multi-threading. I have 2 classes for movement of a crane.
In this cases class there are 2 options, whether the claws ultrasonic sensor sees a box or not (seen down below).
In the other class i have to pick up a box from the initial starting position and put it to another position from 1-3(which is a distance based on keypress).
How do i write the class I am missing?
public class Cases extends Thread
UltrasonicSensor usc= new UltrasonicSensor(SensorPort.S1);
int box=0;
boolean isBoxThere=true;
public Cases cases;
public void error() throws InterruptedException
{
LCD.drawString("There are no boxes", 0, 0);
LCD.drawString(" At the location", 0, 1);
Button.waitForAnyPress();
LCD.clear();
Thread.sleep(1000);
}
//Claw movement
public void down() throws InterruptedException{
MotorPort motorY=MotorPort.B;
motorY.resetTachoCount();
motorY.controlMotor(85, 1);
stopY();
}
public void up() throws InterruptedException{
MotorPort motorY = MotorPort.B;
motorY.controlMotor(85, 2);
while(motorY.getTachoCount()>0); //After the crane grabs the box it lifts it back up
stopY();
}
public void clawclose() throws InterruptedException{
MotorPort motorC=MotorPort.C;
motorC.controlMotor(65, 2);
{
Thread.sleep(505); //Thread sleep is used for the cranes precise stopping
}
motorC.controlMotor(100, 3);
{
Thread.sleep(1000);
}
stopC();
}
public void clawopen() throws InterruptedException{
MotorPort motorC=MotorPort.C;
{
Thread.sleep(520);
}
motorC.controlMotor(100, 3);
{
Thread.sleep(1000);
}
stopC();
}
//Codes for stopping of Motors
public static void stopY () throws InterruptedException{
MotorPort motorY = MotorPort.B;
motorY.controlMotor(100, 3);
Thread.sleep(1000);
}
public static void stopC () throws InterruptedException{
MotorPort motorC = MotorPort.C;
motorC.controlMotor(100, 3);
Thread.sleep(1000);
}
public void run()
{
try
{
while(true)
{
switch(box)
{
case 1:
{
while(box==1)
{
isBoxThere=true;
try {
clawopen();
} catch (InterruptedException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
down();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while(usc.getDistance()<10)
{
try
{
Thread.sleep(100);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
try
{
clawclose();
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
case 2:
{
while(box==0)
{
isBoxThere=false;
try
{
error();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
}
}
finally{}
}
}
If i know a specific thread id. How to do the following?
Thread.getThreadById(id).continueWork();
Is it possible?
public class Test implements Runnable {
public void run() {
while(true){
pause();
doSomework();
}
}
private void doSomework() {
System.out.println("do some work");
}
public synchronized void pause() {
if (Tester.waitCondition == true) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized void continueWork() {
notify();
}
}
public class Tester {
public static boolean waitCondition = true;
public static void main(String[] args) {
Thread nThread = new Thread(new Test());
nThread.start();
waitCondition = false;
Thread nThread1 = new Thread(new Test());
nThread1.start();
Thread nThread2 = new Thread(new Test());
nThread2.start();
Thread nThread3 = new Thread(new Test());
nThread3.start();
Long id = nThread.getId();
Thread.getThreadById(id).continueWork();
}
}
You need block the thread with a lock , then call the lock's notify method to set blocked thread runnable .
If more than one thread to be continued , you will need Condition .
Like blow:
final Lock lock = new ReentrantLock();
final Condition condition1 = lock.newCondition();
final Condition condition2 = lock.newCondition();
Thread t = new Thread() {
#Override
public void run() {
try {
lock.lock();
condition1.await();
System.out.println("end cdt1");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
lock.unlock();
}
}
};
t.start();
Thread t1 = new Thread() {
#Override
public void run() {
try {
lock.lock();
condition2.await();
System.out.println("end cdt2");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
lock.unlock();
}
}
};
t1.start();
Thread.sleep(1000);
Thread tt = new Thread() {
#Override
public void run() {
try {
lock.lock();
condition1.signal();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
lock.unlock();
}
}
};
tt.start();
Thread.sleep(2000);
Thread tt1 = new Thread() {
#Override
public void run() {
try {
lock.lock();
condition2.signal();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
lock.unlock();
}
}
};
tt1.start();
Elaborating:
public class Tester {
// Apologies, I'm too lazy to create two separate files
static public class Test implements Runnable {
private void doSomework() {
System.out.print(
"do some work on Thread: "
+Thread.currentThread().getId()
);
try {
Thread.sleep(500); // just to simulate a load
}
catch(InterruptedException e) {
// ignore
}
}
public void run() {
do {
boolean shouldIWait=true;
synchronized(Tester.lockingObj) {
Boolean flag=Tester.waitConditions.get(Thread.currentThread().getId());
if(null!=flag) {
shouldIWait=flag.booleanValue();
} // if null, the tester started me before creating my flag. I'll wait
if(shouldIWait) {
// I need to wait for someone to wake me
try {
Tester.lockingObj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
// well, I'm interrupted, so I'll do no more work.
break;
}
}
}
if(false==shouldIWait) {
// waiting no more
this.doSomework();
}
} while(true);
}
}
public static Object lockingObj=new Object();
public static TreeMap<Long, Boolean> waitConditions=
new TreeMap<Long, Boolean>();
public static void main(String[] args) {
Thread nThread = new Thread(new Test());
Thread nThread1 = new Thread(new Test());
Thread nThread2 = new Thread(new Test());
Thread nThread3 = new Thread(new Test());
// when starting, all threads will be waiting
waitConditions.put(nThread.getId(), true);
waitConditions.put(nThread.getId(), true);
waitConditions.put(nThread.getId(), true);
waitConditions.put(nThread.getId(), true);
nThread2.start();
nThread1.start();
nThread.start();
nThread3.start();
Long id = nThread.getId();
synchronized (lockingObj) { // when notified, all thread should wakeup
waitConditions.put(id, false); // but only nThread will be allowed to doSomeWork
lockingObj.notifyAll(); // wake up all the threads.
// Those not allowed, will go into
// back waiting
}
try {
// just to have the main thread still running for a while
Thread.sleep(3000);
} catch (InterruptedException e) {
}
// maybe we want to switch of nThread and start another?
synchronized (lockingObj) {
waitConditions.put(id, true);
waitConditions.put(nThread1.getId(), false);
lockingObj.notifyAll();
}
try {
// just to have the main thread still running for a while
Thread.sleep(3000);
} catch (InterruptedException e) {
}
}
}
What I want to do here is to print even numbers by even consumers, odd numbers by odd consumers.
There is an evenodd method which is basically consuming any number and printing (whether even or odd). I have got 2 even consumer threads, 2 odd consumer threads, 2 evenodd consumer threads and 3 producer threads.
I am new to the semaphores concept, and I tried using it. When I remove the evenodd method and related threads I get the correct output i.e. Even numbers by even threads and odd numbers by odd threads. But when I again put the evenodd method and evenodd threads I get a deadlock.
It would be very helpful if anyone can guide me on this, where I am going wrong and how to fix it. Please throw some light on how I can implement the evenodd method so that it would work correctly.
package lab61;
import java.util.LinkedList;
import java.util.Random;
import java.util.concurrent.Semaphore;
public class Process {
Semaphore empty;
Semaphore full;
Semaphore even;
Semaphore odd;
Semaphore mutex;
//Semaphore evenodd;
boolean evenb;
boolean oddb,eo;
LinkedList<Integer> list;
Random random;
public Process(int capacity){
list=new LinkedList<Integer>();
empty=new Semaphore(capacity);
full=new Semaphore(0,true);
mutex=new Semaphore(1,true);
even=new Semaphore(0,true);
evenb=false;
oddb=false;
eo=false;
odd=new Semaphore(0,true);
//evenodd = new Semaphore(0,true);
random=new Random();
}
public void Producer() throws InterruptedException{
while(true){
int a=random.nextInt(100);
empty.acquire();
mutex.acquire();
list.add(a);
System.out.println("Producing "+a);
System.out.println(list);
if((list.get(0)%2)==0){
if(!evenb){
even.release();
evenb = true;
}
}else if((list.get(0)%2)==1){
if(!oddb){
odd.release();
oddb = true;
}
}
/*if(((list.get(0)%2)==0)||((list.get(0)%2)==1)){
if(!eo){
evenodd.release();
eo = true;
}
}*/
mutex.release();
full.release();
}
}
public void consumereven() throws InterruptedException{
while(true){
//Thread.sleep(2000);
even.acquire();
Thread.sleep(2000);
full.acquire();
mutex.acquire();
int value = list.removeFirst();
System.out.println("I am the even consumer "+ value);
evenb = false;
System.out.println(list);
mutex.release();
empty.release();
}
}
public void consumerodd() throws InterruptedException{
while(true){
//Thread.sleep(2000);
odd.acquire();
Thread.sleep(2000);
full.acquire();
//odd.acquire();
mutex.acquire();
int value = list.removeFirst();
System.out.println("I am the odd consumer "+ value);
System.out.println(list);
oddb = false;
mutex.release();
empty.release();
}
}
public void evenodd() throws InterruptedException {
while(true){
full.acquire();
mutex.acquire();
int value = list.removeFirst();
System.out.println("i am the evenodd consumer " + value );
if((list.get(0)%2)==0){
if(oddb=true){
odd.acquire();
oddb=false;
}
if(evenb==false){
even.release();
evenb = true;
}
}
else if((list.get(0)%2)==1){
if(evenb=true){
even.acquire();
evenb=false;
}
if(oddb==false){
odd.release();
oddb = true;
}
}
System.out.println(list);
mutex.release();
empty.release();
}
}
}
package lab61;
import lab6.producer;
public class App {
public static void main(String[] args) throws InterruptedException{
Process p=new Process(10);
Thread producer1=new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
try {
p.Producer();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Thread producer2=new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
try {
p.Producer();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Thread producer3=new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
try {
p.Producer();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Thread consumereven1=new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
try {
p.consumereven();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Thread consumereven2=new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
try {
p.consumereven();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Thread consumerodd1=new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
try {
p.consumerodd();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Thread consumerodd2=new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
try {
p.consumerodd();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Thread evenodd1=new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
try {
p.evenodd();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Thread evenodd2=new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
try {
p.evenodd();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
producer1.start();
producer2.start();
producer3.start();
consumereven1.start();
consumereven2.start();
consumerodd1.start();
consumerodd2.start();
evenodd1.start();
evenodd2.start();
producer1.join();
producer2.join();
producer3.join();
consumereven1.join();
consumereven2.join();
consumerodd1.join();
consumerodd2.join();
evenodd1.join();
evenodd2.join();
}
}
The code can cause a deadlock because consumereven() and consumerodd() methods acquire the even or odd semaphore before the full and mutex semaphores, while the evenodd() method acquires the full and mutex semaphores before the even or odd semaphore. This results in a situation where, say, a thread running consumereven() has the even semaphore but is blocked while waiting on the full semaphore, while a thread running evenodd() has the full semaphore, but is blocked on the even semaphore, so the two threads are deadlocked.
To help prevent deadlocks, when you use multiple locks, it's best always to acquire them in the same order wherever they are used, and release them in the reverse order of acquisition.
Not a Great way of doing it but u could do something like.
import java.util.LinkedList;
import java.util.Random;
import java.util.concurrent.Semaphore;
public class P1 {
Semaphore empty;
Semaphore full;
Semaphore even;
Semaphore odd;
Semaphore mutex;
Semaphore oddeven;
LinkedList<Integer> list;
Random random;
boolean oddb;
boolean evenb;
boolean oddevenb;
public P1(int capacity){
list=new LinkedList<Integer>();
empty=new Semaphore(capacity,true);
full=new Semaphore(0,true);
mutex=new Semaphore(1,true);
even=new Semaphore(0,true);
odd=new Semaphore(0,true);
oddeven=new Semaphore(0,true);
oddb=false;
evenb=false;
oddevenb = false;
random=new Random();
}
public void Producer(String s) throws InterruptedException{
while(true){
int a=random.nextInt(100);
empty.acquire();
mutex.acquire();
list.add(a);
System.out.println("Producing "+a+" By "+s);
System.out.println(list);
int temp=random.nextInt(3)+1;
if(temp==1 ){
if(oddevenb==false){
oddeven.release();
oddevenb=true;
evenb=true;
oddb=true;
}
}
else{
if((list.get(0))%2==0){
if(evenb==false){
even.release();
evenb=true;
oddevenb=true;
}
}else if((list.get(0))%2==1){
if( oddb==false) {
odd.release();
oddevenb=true;
oddb=true;
}
}
}
mutex.release();
full.release();
}
}
public void consumereven(String s) throws InterruptedException{
while(true){
// Thread.sleep(5000);
even.acquire();
full.acquire();
mutex.acquire();
int value=list.removeFirst();
System.out.println("I am the "+ s +" consumer "+value);
evenb=false;
oddevenb=false;
System.out.println(list);
mutex.release();
empty.release();
}
}
public void consumerodd(String s) throws InterruptedException{
while(true){
//Thread.sleep(5000);
odd.acquire();
full.acquire();
mutex.acquire();
int value= list.removeFirst();;
System.out.println("I am the "+s+" consumer "+value);
oddb=false;
oddevenb=false;
System.out.println(list);
mutex.release();
empty.release();
}
}
public void consumeroddeven(String s) throws InterruptedException{
while(true){
//Thread.sleep(5000);
oddeven.acquire();
full.acquire();
mutex.acquire();
int value= list.removeFirst();;
System.out.println("I am the "+s+" "+value);
System.out.println(list);
oddevenb=false;
evenb=false;
oddb=false;
mutex.release();
empty.release();
}
}
}
public class App3 {
public static void main(String[] args) throws InterruptedException{
final P1 p=new P1(10);
Thread producer=new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
try {
p.Producer("p1");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Thread producer2=new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
try {
p.Producer("p2");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Thread producer3=new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
try {
p.Producer("p3");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
},"p3");
Thread consumereven=new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
try {
p.consumereven("even1");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Thread consumereven2=new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
try {
p.consumereven("even2");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Thread consumerodd=new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
try {
p.consumerodd("odd1");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Thread consumerodd2=new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
try {
p.consumerodd("odd2");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Thread consumer=new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
try {
p.consumeroddeven("NC1");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Thread consumer1=new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
try {
p.consumeroddeven("NC2");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
producer.start();
producer2.start();
producer3.start();
consumereven.start();
consumereven2.start();
consumerodd.start();
consumerodd2.start();
consumer.start();
consumer1.start();
producer.join();
producer2.join();
producer3.join();
consumereven.join();
consumereven.join();
consumereven2.join();
consumerodd.join();
consumerodd.join();
consumerodd2.join();
consumer.join();
consumer1.join();
}
}
I've got a problem with a SwingWorker. The application sends a file between client and server but the progressmonitor will not be shown me progress during transmission. Could you tell me what i'm doing wrong and what should i do?
The main class is the same for both applications:
package main;
import java.awt.EventQueue;
public class Main {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
Test2 test2=new Test2();
}
});
}
}
Client:
package main;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class Test2 {
public Test2() {
hostName="localhost";
try {
clientSocket = new Socket(hostName, 1234);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File file=new File("E:/test/123.mp4");
try {
fileInputStream=new FileInputStream(file);
bis=new BufferedInputStream(fileInputStream);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
bos=new BufferedOutputStream(clientSocket.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bit=new byte[512];
int len;
System.out.println("Send..."); //test
try {
while ((len = bis.read(bit,0,511)) != -1) {
bos.write(bit, 0, len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
bis.close();
bos.close();
fileInputStream.close();
//fileOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Finish"); //test
}
private String hostName;
private Socket clientSocket;
private BufferedInputStream bis;
private BufferedOutputStream bos;
private FileInputStream fileInputStream;
private byte bit[];
}
Server:
package main;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;
import javax.swing.ProgressMonitor;
import javax.swing.SwingWorker;
public class Test2 {
public Test2() {
pm=new ProgressMonitor(null, "Download...", null, 0, 1850297);
pm.setMillisToDecideToPopup(1);
test4=new Test4();
test4.execute();
}
private class Test4 extends SwingWorker<Boolean, Void> {
public Test4() {
try {
welcomeSocket = new ServerSocket(1234);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Server works");
try {
connectionSocket = welcomeSocket.accept();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
protected Boolean doInBackground() throws Exception {
try {
bis=new BufferedInputStream(connectionSocket.getInputStream());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fileOutputStream=new FileOutputStream("E:/test2/123.mp4");
bos=new BufferedOutputStream(fileOutputStream);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bit=new byte[512];
int len;
System.out.println("Download..."); //test
try {
while ((len = bis.read(bit,0,511)) != -1) {
bos.write(bit, 0, len);
publish();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
bis.close();
bos.close();
fileOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
#Override
protected void process(List<Void> chunks) {
number++;
pm.setProgress(number);
}
#Override
protected void done() {
System.out.println("DONE");
}
}
private ServerSocket welcomeSocket;
private Socket connectionSocket;
private FileOutputStream fileOutputStream;
private BufferedInputStream bis;
private BufferedOutputStream bos;
private byte bit[];
private ProgressMonitor pm;
private Test4 test4;
private int number;
}
Apart from SwingWorker, I've got a problem with a code below. This is second version my server's application without a SwingWorker. Here, the progressmonitor is shown me progress during transmission, but not ever. I used invokeLater in run() method but sometimes the progressmonitor isn't work. Could you tell me what i'm doing wrong?
Server:
package main;
import java.awt.EventQueue;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;
import javax.swing.ProgressMonitor;
import javax.swing.SwingWorker;
public class Test2 {
public Test2() {
pm=new ProgressMonitor(null, "Download...", null, 0, 1850297);
pm.setMillisToDecideToPopup(1);
test3=new Test3();
new Thread(test3).start();
}
private class Test3 implements Runnable {
public Test3() {
}
#Override
public void run() {
try {
welcomeSocket = new ServerSocket(1234);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Server works"); //test
while(true){
try {
connectionSocket = welcomeSocket.accept();
try {
bis=new BufferedInputStream(connectionSocket.getInputStream());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fileOutputStream=new FileOutputStream("E:/test2/123.mp4");
bos=new BufferedOutputStream(fileOutputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
bit=new byte[512];
int len;
System.out.println("Download..."); //test
try {
while ((len = bis.read(bit,0,511)) != -1) {
bos.write(bit, 0, len);
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
number++;
pm.setProgress(number);
}
});
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
bis.close();
bos.close();
fileOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("DONE"); //test
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
private ServerSocket welcomeSocket;
private Socket connectionSocket;
private FileOutputStream fileOutputStream;
private BufferedInputStream bis;
private BufferedOutputStream bos;
private byte bit[];
private ProgressMonitor pm;
private Test3 test3;
private int number;
}
You're waiting on the socket connection and accepting it in the Test4 constructor which will be called on the Swing event thread. It's OK to create the socket there, but don't wait for the connection there as this blocks. Instead wait for the connection it in the doInBackground method.
Also consider changing your SwingWorker so it passes the bytes read:
// change generic parameter to Integer
private class Test4 extends SwingWorker<Boolean, Integer> {
#Override
protected Boolean doInBackground() throws Exception {
// .....
bit=new byte[512];
int len;
try {
while ((len = bis.read(bit,0,511)) != -1) {
bos.write(bit, 0, len);
publish(len);
}
and elsewhere:
#Override
protected void process(List<Integer> chunks) {
// number++;
for (Integer chunk : chunks) {
pm.setProgress(chunk);
}
}
I wrote program that listen Clipboard and worked correctly, but i saw that the program use more CPU usage. CPU usage is more 70%. Why? Can i reduce it? or i want Listener that listen windows's copy action. Is there Listener in java for this?
This is my code:
package yoxlama;
import java.awt.*;
import java.awt.datatransfer.*;
import java.io.IOException;
import java.net.MalformedURLException;
class ClipBoardListener extends Thread implements ClipboardOwner {
Clipboard sysClip = Toolkit.getDefaultToolkit().getSystemClipboard();
static ClipBoardListener b;
Interlsoft interlsoft = new Interlsoft();
public void run() {
try{
Transferable trans = sysClip.getContents(this);
regainOwnership(trans);
}catch (Exception e) {
try {
b.finalize();
} catch (Throwable e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
b= new ClipBoardListener();
b.start();
System.out.println("NESE1");
}
System.out.println("Listening to board...");
while(true) {}
}
public void lostOwnership(Clipboard c, Transferable t) {
try {
Thread.sleep(50);
} catch(Exception e) {
System.out.println("Exception: " + e);
}
try{
Transferable contents = sysClip.getContents(this);
processContents(contents);
regainOwnership(contents);
}catch (Exception e) {
try {
b.finalize();
b= new ClipBoardListener();
b.start();
} catch (Throwable e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("nese");
}
}
void processContents(Transferable t) throws MalformedURLException, IOException {
String str = getClipboard(t);
if(str!=null){
str= str.replace("\n", "%0D%0A");
str= str.replace("\r", "");
str= str.replace("\t", "");
str= str.replace("/", "");
str= str.replace("-", "");
interlsoft.translate(str);
}
// System.out.println("Processing: " + getClipboard(t));
}
void regainOwnership(Transferable t) {
sysClip.setContents(t, this);
}
public static void main(String[] args) {
b = new ClipBoardListener();
b.start();
}
public static String getClipboard(Transferable t) {
// Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
try {
if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
String text = (String)t.getTransferData(DataFlavor.stringFlavor);
return text;
}
} catch (UnsupportedFlavorException e) {
System.out.println("BURDA1");
} catch (IOException e) {
System.out.println("BURDA1");
}
return null;
}
}
This: while(true) {}
Your poor CPU. Instead of while true, just make that thread sleep indefinitely. See: How do you hang a thread in Java in one line?