Checking the AsyncTask status seems not working correctly (log doesn't
appear on log cat)
I'm trying to see how works an Asynctask class in android. In particular i
want reveal in real time the status of the class for see when it is
running and when it has finished. For do this, i have created a class that
extend the main activity and another class that is the asynctaks class.
This is my main class:
public class PhotoManagement extends Activity{
private String numberOfSelectedPhotos;
private Bitmap currentImage;
private String initConfiguration = "http://www.something.com";
private String response;
private ArrayList<String> formatPhotoList = new ArrayList<String>();
//create a list that will contains the available format of the photos
downloaded from the server
private ArrayList<String> pricePhotoList = new ArrayList<String>();
//create a list that will contains the available price for each format of
the photos
DownloadWebPageTask webPage = new DownloadWebPageTask();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void onResume(){
super.onResume();
new DownloadWebPageTask().execute(initConfiguration);
if(webPage.getStatus() == AsyncTask.Status.PENDING){
Log.i("STATUS","PENDING");
}
if(webPage.getStatus() == AsyncTask.Status.RUNNING){
Log.i("","RUNNING");
}
if(webPage.getStatus() == AsyncTask.Status.FINISHED){
Log.i("","FINISHED");
}
}
}
As you can see i want only see the passages of the status with a simple
log. And here there is the asynctask class.
private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
for (String url : urls) {
DefaultHttpClient client = new DefaultHttpClient(); //create a
new http client
HttpGet httpGet = new HttpGet(url); //create a new http
request passing a valid url
try {
HttpResponse execute = client.execute(httpGet); //try to
execute the http get request
InputStream content = execute.getEntity().getContent();
//prepare the input stream to read the bytes of the
request
BufferedReader buffer = new BufferedReader(
new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null) {
response += s; //until is present a line to read, the
response variable store the value of the lines
}
} catch (Exception e) {
Log.i("MyApp", "Download Exception : " + e.toString());
//Print the error if something goes wrong
}
}
return response; //return the response
}
@Override
protected void onPostExecute(String result) {
result = doInBackground(initConfiguration); //take the result from
the DownloadWebPageTask class
result = result.replace("null", "");
Log.i("RESULT",""+result);
//find the price and format value from the result using XmlPullParser
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput( new StringReader ( result ) );
int attributeNumber = xpp.getAttributeCount();
int eventType = xpp.getEventType();
String currentTag = null;
while(eventType != XmlPullParser.END_DOCUMENT){
if(eventType == XmlPullParser.START_TAG) {
currentTag = xpp.getName();
if (currentTag.equals("product")){
xpp.getAttributeValue(null, "name");
formatPhotoList.add(xpp.getAttributeValue(null,
"name"));
Log.i("FORMAT
PHOTO",""+xpp.getAttributeValue(null, "name"));
}
}
eventType = xpp.next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
Log.i("","ERROR XML PULL PARSER");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i("","ERROR IOEXCEPTION");
}
}
}
}
As you can see i have implemented also the method onPostExecute that
should be called when the asynctask method has finished to execute the
instructions right? So at this point i don't understand why my log RUNNING
and my log FINISHED never appear on the log cat. What i'm doing wrong? I'm
tried to follow this topic Android, AsyncTask, check status? but in my
case it isn't working.
Thanks
Thursday, 3 October 2013
Wednesday, 2 October 2013
HTML linking titles and subtitles in php
HTML linking titles and subtitles in php
I am trying to make a wikipedia like thing that makes a little list with
clickable links of all the html titles <h1></h1> and <h2></h2>
So
<h1>Title</h1>
[random text about things]
<h2>subtitle</h2>
[random text]
<h2>subtitle2</h2>
<h1>Title2</h1>
[random text about things]
<h2>subtitle3</h2>
[random text]
<h2>subtitle4</h2>
would become
Title - subtitle - subtitle2 Title2 - subtitle3 - subtitle4
with all subtitles being links to that html part.
I'm thinking myself of preg_replace things, but not sure if that is the
best way. Tips?
I am trying to make a wikipedia like thing that makes a little list with
clickable links of all the html titles <h1></h1> and <h2></h2>
So
<h1>Title</h1>
[random text about things]
<h2>subtitle</h2>
[random text]
<h2>subtitle2</h2>
<h1>Title2</h1>
[random text about things]
<h2>subtitle3</h2>
[random text]
<h2>subtitle4</h2>
would become
Title - subtitle - subtitle2 Title2 - subtitle3 - subtitle4
with all subtitles being links to that html part.
I'm thinking myself of preg_replace things, but not sure if that is the
best way. Tips?
Best Way to Make AJAX Call from WebForms Page
Best Way to Make AJAX Call from WebForms Page
Is there a recommended way to make an AJAX call within a WebForms
application?
I know there is the built-in ASP.NET AJAX components, but they seem a bit
heavy. I'm used to doing this in MVC and it seems very clean.
I can use Page Methods, but they require that my method is static, which
makes it more difficult to access my database, etc.
I assume I can also just use jQuery to make the call, although attempts at
this have failed in the past, usually due to problems with the way data
was returned (JSON, etc.).
My goal is get pass a string fragment and get back a list of items that
match that fragment. Speed would be nice. Can I get some recommendations?
Is there a recommended way to make an AJAX call within a WebForms
application?
I know there is the built-in ASP.NET AJAX components, but they seem a bit
heavy. I'm used to doing this in MVC and it seems very clean.
I can use Page Methods, but they require that my method is static, which
makes it more difficult to access my database, etc.
I assume I can also just use jQuery to make the call, although attempts at
this have failed in the past, usually due to problems with the way data
was returned (JSON, etc.).
My goal is get pass a string fragment and get back a list of items that
match that fragment. Speed would be nice. Can I get some recommendations?
Tuesday, 1 October 2013
using singleton for creating listener object not working
using singleton for creating listener object not working
i am using a singleton that for some reason is not working, can't figure
out why
private static ConnectionUtility instance;
public static ConnectionUtility getInstance() {
if(instance == null){
instance = new ConnectionUtility();
}
return instance;
}
in the code shown above, the second time the execution of this code takes
place, instance is not null, an instance has been created already, so on
the second time this code is executed it should go to the return instance
line directly and skip the instance = new ConnectionUtility() line.
however on second iteration it will try to create another instance of the
ConnectionUtility object when one already exists. why is it doing this?
how to fix this problem?
full code posted below: for 2 classes ConnectionUtility and MultiThreader
public class ConnectionUtility extends javax.swing.JFrame
implements MultiThreader.OnSendResultListener {
String outputLine = "";
boolean runner = true;
PrintWriter out;
BufferedReader in;
ServerSocket serversocket;
static Socket socket;
boolean startServer = true;
public static String displayString = "";
private static ConnectionUtility instance;
static int counter = 0;
public static ConnectionUtility getInstance() {
if(instance == null){
instance = new ConnectionUtility();
}
return instance;
}
private ConnectionUtility() {
initComponents();
this.setVisible(true);
serverRunner();
File fileOne = new File("C:/DBFiles");
if(!fileOne.exists()){
fileOne.mkdir();
}
File fileTwo = new File("C:/DBFilesOut");
if(!fileTwo.exists()){
fileTwo.mkdir();
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("MS UI Gothic", 0, 36)); // NOI18N
jLabel1.setText("TankInspectionSystem");
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(72, 72, 72)
.addComponent(jLabel1)
.addContainerGap(76, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 383,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(37, 37, 37)
.addComponent(jLabel1)
.addGap(34, 34, 34)
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 179,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(37, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration
public void serverRunner(){
runner = true;
try {
serversocket = new ServerSocket(6789, 100);
System.out.println();
} catch (IOException ex) {
Logger.getLogger(ConnectionUtility.class.getName()).log(Level.SEVERE,
null, ex);
}
while(runner){
try {
socket = serversocket.accept();
addAndDisplayTextToString("new connection, inet socket address >>> " +
socket.getPort());
System.out.println(displayString);
} catch (IOException ex) {
Logger.getLogger(ConnectionUtility.class.getName()).log(Level.SEVERE,
null, ex);
}
// MultiThreader multi = new MultiThreader(socket, this);
MultiThreader multi = new MultiThreader(socket);
Thread t = new Thread(multi);
t.start();
} // end while runner loop
} // end serverRunner method
public static void addAndDisplayTextToString(String setString){
StringBuilder stb = new StringBuilder(displayString);
setString = setString + "\n";
if(stb.toString() == ""){
stb.append(setString);
}else if(stb.toString() != ""){
stb.insert(0, setString);
}
int counter = 0;
for(int i = 0; i < stb.length(); i++){
if(stb.substring(i, i + 1).equals("\n")){
counter++;
}
}
// get the last index of "\n"
int lastIndex = stb.lastIndexOf("\n");
int maximum = 4;
if(counter >= maximum){
stb.delete(lastIndex, stb.length());
System.out.println();
}
displayString = stb.toString();
}
@Override
public void onStringResult(String transferString) {
addAndDisplayTextToString(transferString);
jTextArea1.setText(displayString);
System.out.println("RETURNED TRING " + transferString);
}
} // class ConnectionUtility
public class MultiThreader implements Runnable {
private Socket socket;
public int fileSizeFromClient;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
DataInputStream dis = null;
DataOutputStream dos = null;
ScheduledThreadPoolExecutor stpe;
private OnSendResultListener listener;
public MultiThreader(Socket s) {
// public MultiThreader(Socket s, OnSendResultListener resultListener){
socket = s;
stpe = new ScheduledThreadPoolExecutor(5);
listener = ConnectionUtility.getInstance();
// listener = resultListener;
}
@Override
public void run() {
long serialNumber = 0;
int bufferSize = 0;
// get input streams
try {
bis = new BufferedInputStream(socket.getInputStream());
dis = new DataInputStream(bis);
} catch (IOException ex) {
Logger.getLogger(MultiThreader.class.getName()).log(Level.SEVERE,
null, ex);
}
sendStatus("New connection strarted");
// read in streams from server
try {
fileSizeFromClient = dis.readInt();
sendStatus("File size from client " + fileSizeFromClient);
serialNumber = dis.readLong();
sendStatus("Serial mumber from client " + serialNumber);
} catch (IOException ex) {
Logger.getLogger(MultiThreader.class.getName()).log(Level.SEVERE,
null, ex);
}
try {
bufferSize = socket.getReceiveBufferSize();
sendStatus("Buffer size " + bufferSize);
} catch (SocketException ex) {
Logger.getLogger(MultiThreader.class.getName()).log(Level.SEVERE,
null, ex);
}
String serialString = String.valueOf(serialNumber);
File fileDirectory = new File("C:" + File.separator + "DOWNLOAD" +
File.separator + serialNumber + File.separator);
fileDirectory.mkdir();
File file = new File("C:" + File.separator + "DOWNLOAD" +
File.separator + serialNumber + File.separator + "JISSend.pdf");
try {
file.createNewFile();
} catch (IOException ex) {
Logger.getLogger(MultiThreader.class.getName()).log(Level.SEVERE,
null, ex);
}
try {
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
dos = new DataOutputStream(bos);
} catch (FileNotFoundException ex) {
Logger.getLogger(MultiThreader.class.getName()).log(Level.SEVERE,
null, ex);
}
int count = 0;
byte[] buffer = new byte[fileSizeFromClient];
try {
int totalBytesRead = 0;
while(totalBytesRead < fileSizeFromClient){
int bytesRemaining = fileSizeFromClient - totalBytesRead;
int bytesRead = dis.read(buffer, 0, (int)Math.min(buffer.length,
bytesRemaining));
if(bytesRead == -1){
break;
}else{
dos.write(buffer, 0, bytesRead);
totalBytesRead += bytesRead;
}
}
} catch (IOException ex) {
Logger.getLogger(MultiThreader.class.getName()).log(Level.SEVERE,
null, ex);
}
try {
dos = new DataOutputStream(socket.getOutputStream());
} catch (IOException ex) {
Logger.getLogger(MultiThreader.class.getName()).log(Level.SEVERE,
null, ex);
}
stpe.schedule(new CompareFiles(), 0, TimeUnit.SECONDS);
stpe.schedule(new CloseResources(), 2, TimeUnit.SECONDS);
} // end run method
public class CompareFiles implements Runnable {
@Override
public void run() {
int returnInt = 0;
FileInputStream fis = null;
File file = new File("C:/DOWNLOAD/JISSend.pdf");
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException ex) {
Logger.getLogger(MultiThreader.class.getName()).log(Level.SEVERE,
null, ex);
}
int fileLength = (int) file.length();
sendStatus("Size of database file sent " + fileLength);
if(fileLength == fileSizeFromClient){
sendStatus("File sent to server, Successful");
returnInt = 1;
}else if(fileLength != fileSizeFromClient){
sendStatus("ERROR, file send failed");
returnInt = 2;
}
try {
dos.writeInt(returnInt);
} catch (IOException ex) {
Logger.getLogger(MultiThreader.class.getName()).log(Level.SEVERE,
null, ex);
}
} // end run method
} // end of class comparefiles
public class CloseResources implements Runnable {
@Override
public void run() {
try {
fos.flush();
bis.close();
bos.close();
dis.close();
dos.close();
socket.close();
} catch (IOException ex) {
Logger.getLogger(MultiThreader.class.getName()).log(Level.SEVERE,
null, ex);
}
} // end run method
} // end of class closeResources
public interface OnSendResultListener {
public void onStringResult(String transferString);
}
public void sendStatus(String status){
listener.onStringResult(status);
}
} // end class mulitthreader
i am using a singleton that for some reason is not working, can't figure
out why
private static ConnectionUtility instance;
public static ConnectionUtility getInstance() {
if(instance == null){
instance = new ConnectionUtility();
}
return instance;
}
in the code shown above, the second time the execution of this code takes
place, instance is not null, an instance has been created already, so on
the second time this code is executed it should go to the return instance
line directly and skip the instance = new ConnectionUtility() line.
however on second iteration it will try to create another instance of the
ConnectionUtility object when one already exists. why is it doing this?
how to fix this problem?
full code posted below: for 2 classes ConnectionUtility and MultiThreader
public class ConnectionUtility extends javax.swing.JFrame
implements MultiThreader.OnSendResultListener {
String outputLine = "";
boolean runner = true;
PrintWriter out;
BufferedReader in;
ServerSocket serversocket;
static Socket socket;
boolean startServer = true;
public static String displayString = "";
private static ConnectionUtility instance;
static int counter = 0;
public static ConnectionUtility getInstance() {
if(instance == null){
instance = new ConnectionUtility();
}
return instance;
}
private ConnectionUtility() {
initComponents();
this.setVisible(true);
serverRunner();
File fileOne = new File("C:/DBFiles");
if(!fileOne.exists()){
fileOne.mkdir();
}
File fileTwo = new File("C:/DBFilesOut");
if(!fileTwo.exists()){
fileTwo.mkdir();
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("MS UI Gothic", 0, 36)); // NOI18N
jLabel1.setText("TankInspectionSystem");
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(72, 72, 72)
.addComponent(jLabel1)
.addContainerGap(76, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 383,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(37, 37, 37)
.addComponent(jLabel1)
.addGap(34, 34, 34)
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 179,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(37, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration
public void serverRunner(){
runner = true;
try {
serversocket = new ServerSocket(6789, 100);
System.out.println();
} catch (IOException ex) {
Logger.getLogger(ConnectionUtility.class.getName()).log(Level.SEVERE,
null, ex);
}
while(runner){
try {
socket = serversocket.accept();
addAndDisplayTextToString("new connection, inet socket address >>> " +
socket.getPort());
System.out.println(displayString);
} catch (IOException ex) {
Logger.getLogger(ConnectionUtility.class.getName()).log(Level.SEVERE,
null, ex);
}
// MultiThreader multi = new MultiThreader(socket, this);
MultiThreader multi = new MultiThreader(socket);
Thread t = new Thread(multi);
t.start();
} // end while runner loop
} // end serverRunner method
public static void addAndDisplayTextToString(String setString){
StringBuilder stb = new StringBuilder(displayString);
setString = setString + "\n";
if(stb.toString() == ""){
stb.append(setString);
}else if(stb.toString() != ""){
stb.insert(0, setString);
}
int counter = 0;
for(int i = 0; i < stb.length(); i++){
if(stb.substring(i, i + 1).equals("\n")){
counter++;
}
}
// get the last index of "\n"
int lastIndex = stb.lastIndexOf("\n");
int maximum = 4;
if(counter >= maximum){
stb.delete(lastIndex, stb.length());
System.out.println();
}
displayString = stb.toString();
}
@Override
public void onStringResult(String transferString) {
addAndDisplayTextToString(transferString);
jTextArea1.setText(displayString);
System.out.println("RETURNED TRING " + transferString);
}
} // class ConnectionUtility
public class MultiThreader implements Runnable {
private Socket socket;
public int fileSizeFromClient;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
DataInputStream dis = null;
DataOutputStream dos = null;
ScheduledThreadPoolExecutor stpe;
private OnSendResultListener listener;
public MultiThreader(Socket s) {
// public MultiThreader(Socket s, OnSendResultListener resultListener){
socket = s;
stpe = new ScheduledThreadPoolExecutor(5);
listener = ConnectionUtility.getInstance();
// listener = resultListener;
}
@Override
public void run() {
long serialNumber = 0;
int bufferSize = 0;
// get input streams
try {
bis = new BufferedInputStream(socket.getInputStream());
dis = new DataInputStream(bis);
} catch (IOException ex) {
Logger.getLogger(MultiThreader.class.getName()).log(Level.SEVERE,
null, ex);
}
sendStatus("New connection strarted");
// read in streams from server
try {
fileSizeFromClient = dis.readInt();
sendStatus("File size from client " + fileSizeFromClient);
serialNumber = dis.readLong();
sendStatus("Serial mumber from client " + serialNumber);
} catch (IOException ex) {
Logger.getLogger(MultiThreader.class.getName()).log(Level.SEVERE,
null, ex);
}
try {
bufferSize = socket.getReceiveBufferSize();
sendStatus("Buffer size " + bufferSize);
} catch (SocketException ex) {
Logger.getLogger(MultiThreader.class.getName()).log(Level.SEVERE,
null, ex);
}
String serialString = String.valueOf(serialNumber);
File fileDirectory = new File("C:" + File.separator + "DOWNLOAD" +
File.separator + serialNumber + File.separator);
fileDirectory.mkdir();
File file = new File("C:" + File.separator + "DOWNLOAD" +
File.separator + serialNumber + File.separator + "JISSend.pdf");
try {
file.createNewFile();
} catch (IOException ex) {
Logger.getLogger(MultiThreader.class.getName()).log(Level.SEVERE,
null, ex);
}
try {
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
dos = new DataOutputStream(bos);
} catch (FileNotFoundException ex) {
Logger.getLogger(MultiThreader.class.getName()).log(Level.SEVERE,
null, ex);
}
int count = 0;
byte[] buffer = new byte[fileSizeFromClient];
try {
int totalBytesRead = 0;
while(totalBytesRead < fileSizeFromClient){
int bytesRemaining = fileSizeFromClient - totalBytesRead;
int bytesRead = dis.read(buffer, 0, (int)Math.min(buffer.length,
bytesRemaining));
if(bytesRead == -1){
break;
}else{
dos.write(buffer, 0, bytesRead);
totalBytesRead += bytesRead;
}
}
} catch (IOException ex) {
Logger.getLogger(MultiThreader.class.getName()).log(Level.SEVERE,
null, ex);
}
try {
dos = new DataOutputStream(socket.getOutputStream());
} catch (IOException ex) {
Logger.getLogger(MultiThreader.class.getName()).log(Level.SEVERE,
null, ex);
}
stpe.schedule(new CompareFiles(), 0, TimeUnit.SECONDS);
stpe.schedule(new CloseResources(), 2, TimeUnit.SECONDS);
} // end run method
public class CompareFiles implements Runnable {
@Override
public void run() {
int returnInt = 0;
FileInputStream fis = null;
File file = new File("C:/DOWNLOAD/JISSend.pdf");
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException ex) {
Logger.getLogger(MultiThreader.class.getName()).log(Level.SEVERE,
null, ex);
}
int fileLength = (int) file.length();
sendStatus("Size of database file sent " + fileLength);
if(fileLength == fileSizeFromClient){
sendStatus("File sent to server, Successful");
returnInt = 1;
}else if(fileLength != fileSizeFromClient){
sendStatus("ERROR, file send failed");
returnInt = 2;
}
try {
dos.writeInt(returnInt);
} catch (IOException ex) {
Logger.getLogger(MultiThreader.class.getName()).log(Level.SEVERE,
null, ex);
}
} // end run method
} // end of class comparefiles
public class CloseResources implements Runnable {
@Override
public void run() {
try {
fos.flush();
bis.close();
bos.close();
dis.close();
dos.close();
socket.close();
} catch (IOException ex) {
Logger.getLogger(MultiThreader.class.getName()).log(Level.SEVERE,
null, ex);
}
} // end run method
} // end of class closeResources
public interface OnSendResultListener {
public void onStringResult(String transferString);
}
public void sendStatus(String status){
listener.onStringResult(status);
}
} // end class mulitthreader
why is this an example of delegation but not aggregation in Java?
why is this an example of delegation but not aggregation in Java?
I found this example in this SO for delegation. I fail to see why this not
an aggregation relationship? The Secretary object continues to exist even
if boss object is destroyed
public interface Worker() {
public Result work();
}
public class Secretary() implements Worker {
public Result work() {
Result myResult = new Result();
return myResult;
}
}
can someone explain why this is delegation but not aggregation?
public class Boss() implements Worker {
private Secretary secretary;
public Result work() {
return secretary.work();
}
}
I found this example in this SO for delegation. I fail to see why this not
an aggregation relationship? The Secretary object continues to exist even
if boss object is destroyed
public interface Worker() {
public Result work();
}
public class Secretary() implements Worker {
public Result work() {
Result myResult = new Result();
return myResult;
}
}
can someone explain why this is delegation but not aggregation?
public class Boss() implements Worker {
private Secretary secretary;
public Result work() {
return secretary.work();
}
}
Screen shuts off when using proprietary NVidia drivers
Screen shuts off when using proprietary NVidia drivers
pAs soon as I install the proprietary NVidia driver on my laptop (even
after a fresh Kubuntu install), I start to experience random screen
failures, that is, the screen suddenly shuts off, the backlight is still
on, but the display is black, and trying to switch tty changes nothing./p
pHaving skimmed through Xorg.log, I couldn't find any error or warning
that could be related (but then, I am no expert), but there are some 200
NULL (^@) chars at the end, and I don't know if it is normal./p pI have no
such problem when using nouveau, so I suspect it is a driver issue, not a
physical one./p pI am using nvidia-current (so 304.88, but the issue
occurs with every driver available in jockey, that is another 304 and two
319) on Kubuntu Saucy (but the issue occurs on Raring too) with a GeForce
8800M GTX./p pWhat should I do to make the proprietary drivers work?/p
pAs soon as I install the proprietary NVidia driver on my laptop (even
after a fresh Kubuntu install), I start to experience random screen
failures, that is, the screen suddenly shuts off, the backlight is still
on, but the display is black, and trying to switch tty changes nothing./p
pHaving skimmed through Xorg.log, I couldn't find any error or warning
that could be related (but then, I am no expert), but there are some 200
NULL (^@) chars at the end, and I don't know if it is normal./p pI have no
such problem when using nouveau, so I suspect it is a driver issue, not a
physical one./p pI am using nvidia-current (so 304.88, but the issue
occurs with every driver available in jockey, that is another 304 and two
319) on Kubuntu Saucy (but the issue occurs on Raring too) with a GeForce
8800M GTX./p pWhat should I do to make the proprietary drivers work?/p
Approximation using Euler's method.
Approximation using Euler's method.
Consider the initial value problem $$\dfrac{dy}{dx} = y,y(0) =1$$
Approximate $y(1)$ using Euler's method with a step size of
$\dfrac{1}{n}$, where $n$ is an arbitrary natural number. Use this
approximation to write Euler's number $e$ as a limit of an expression in
$n$. How large do you have to choose $n$ in order to approximate $e$ up to
an error of at most 0.1? Comment on the quality of approximate in this
example.
What I did is the following:
$y(1) \approx y_1 = y_0 +hf(x_0,y_0) = 1+hf(0,1) = 1+\dfrac{1}{n}$
This is where I stuck, am I on the right direction? What should I do next?
Consider the initial value problem $$\dfrac{dy}{dx} = y,y(0) =1$$
Approximate $y(1)$ using Euler's method with a step size of
$\dfrac{1}{n}$, where $n$ is an arbitrary natural number. Use this
approximation to write Euler's number $e$ as a limit of an expression in
$n$. How large do you have to choose $n$ in order to approximate $e$ up to
an error of at most 0.1? Comment on the quality of approximate in this
example.
What I did is the following:
$y(1) \approx y_1 = y_0 +hf(x_0,y_0) = 1+hf(0,1) = 1+\dfrac{1}{n}$
This is where I stuck, am I on the right direction? What should I do next?
Subscribe to:
Comments (Atom)